天天看點

模闆語言-Velocity

表現層技術主要有三種:jsp、freemarker、velocity

velocity是較早出現的用于代替jsp的模闆語言

優點:

1、不能編寫java代碼,可以實作嚴格的mvc分離

2、性能良好,據說比jsp性能還要好些

3、使用表達式語言,據說jsp的表達式語言就是學velocity的

缺點:

1、不是官方标準 2、使用者群體和第三方标簽庫沒有jsp多 3、對jsp标簽支援不夠好

詳細文法介紹: https://www.cnblogs.com/avivaye/p/4418878.html

一般web開發中使用模闆語言的場景:郵件,生成合同,協定等

測試項目

pom.xml:引入必須的包

<dependency>
    <groupId>org.apache.velocity</groupId>
    <artifactId>velocity</artifactId>
    <version>1.7</version>
</dependency>
           

測試類:

@RunWith(SpringRunner.class)
@SpringBootTest
public class VelocityApplicationTests {

    @Test
    public void contextLoads() {
    	//context:就是文本,實際應用此文本可以從檔案中讀取或者資料庫中讀取
        String context = "名稱:$realName";
        VelocityEngine velocityEngine = new VelocityEngine();
        VelocityContext velocityContext = new VelocityContext();
        //給$realName 指派  $realName  意思是如果realName 為null 頁面顯示空值,其他文法參考上面的連接配接
        velocityContext.put("realName", "ethan");
        StringWriter templateWriter = new StringWriter();
        velocityEngine.evaluate(velocityContext, templateWriter, "", context);
        System.out.println(templateWriter.toString());
    }

}
           

運作輸出結果:

模闆語言-Velocity