velocity基本使用

1. Velocity 開發(fā)

apache Velocity開發(fā)指導(dǎo)傳送門

1.1 Velocity使用流程

  • 初始化Velocity(單例或者多實(shí)例)
  • 創(chuàng)建一個context
  • 向context中添加對象
  • 選擇模版
  • Merge 模版和context

1.2 pom依賴

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

1.3不同模式下使用velocity

  • 單例模式使用: org.apache.velocity.app.Velocity
  • 多實(shí)例模式使用: org.apache.velocity.app.VelocityEngine

1.3.1 單例模式使用velocity

//初始化Velocity
Velocity.init();
//創(chuàng)建context
VelocityContext context = new VelocityContext();
//添加數(shù)據(jù)
context.put( "name", new String("Velocity") );
//獲取模版
Template template = Velocity.getTemplate("templates/helloworld.vm");
//Merge 模版和context
StringWriter writer = new StringWriter();
template.merge(context, writer);
System.out.println(writer.toString());

1.3.2 多實(shí)例使用velocity

//每次創(chuàng)建VelocityEngine對象
VelocityEngine ve = new VelocityEngine();
ve.setProperty( VelocityEngine.RUNTIME_LOG_LOGSYSTEM, this);
ve.init();
//創(chuàng)建context
VelocityContext context = new VelocityContext();
//添加數(shù)據(jù)
context.put( "name", new String("Velocity") );
//獲取模版
Template template = Velocity.getTemplate("templates/helloworld.vm");
//Merge 模版和context
StringWriter writer = new StringWriter();
template.merge(context, writer);
System.out.println(writer.toString());

1.4 Context

1.4.1 Context 基本概念

Context是作為數(shù)據(jù)容器的,實(shí)現(xiàn)是基于HashMap的,模版中用到的變量都必須放到context中,類似于spring中的ModelAndView。沒有特殊需求的話可以使用官方推薦的VelocityContext,不支持jdk對象序列化。
VelocityContext構(gòu)造器默認(rèn)實(shí)現(xiàn)HashMap:

    /**
     *  Initializes internal storage (never to <code>null</code>), and
     *  inner context.
     *
     *  @param context Internal storage, or <code>null</code> to
     *  create default storage.
     *  @param innerContext Inner context.
     */
    public VelocityContext(Map context, Context innerContext)
    {
        super(innerContext);
        //如果conext 為空新建一個
        this.context = (context == null ? new HashMap() : context);
    }
```
context的添加獲取操作:
```
    /**
     * Adds a name/value pair to the context.
     *
     * @param key   The name to key the provided value with.
     * @param value The corresponding value.
     * @return The old object or null if there was no old object.
     */
    Object put(String key, Object value);

    /**
     * Gets the value corresponding to the provided key from the context.
     *
     * @param key The name of the desired value.
     * @return    The value corresponding to the provided key.
     */
    Object get(String key);
```
### 1.4.2.1 #foreach( ) 支持的數(shù)據(jù)結(jié)構(gòu)
- 實(shí)現(xiàn)Iterable接口的對象
- 數(shù)組會被velocity包裝成實(shí)現(xiàn)Iterable的類,v1.6之后可以把數(shù)組當(dāng)成list來看待,可以使用size(), isEmpty() and get(int)方法。
- Map values()方法需要返回有用的信息
- Enumeration只能使用一次,沒有復(fù)位會報錯

### 1.4.2.2 支持static類
不是所有的類都可以創(chuàng)建對象,比如Math類沒有公有的構(gòu)造器,要用到Math的方法可以直接把math的class對象傳過去。
```
context.put("Math", Math.class);
```
## 1.4.2.3 context嵌套
```
VelocityContext context1 = new VelocityContext();

context1.put("name","Velocity");
context1.put("project", "Jakarta");
context1.put("duplicate", "I am in context1");

VelocityContext context2 = new VelocityContext( context1 );

context2.put("lang", "Java" );
context2.put("duplicate", "I am in context2");

template.merge( context2, writer );
```
- 如果有重復(fù)的key最后實(shí)際上存的是最后一個key的值,上面代碼中g(shù)et duplicate會得到I am in context2
- 重復(fù)的key不會影響原有的context中的值,context1.get("duplicate")獲取到的還是I am in context1
- #set()可以影響到外層的context的值,但是不影響內(nèi)層的

















最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • Spring Cloud為開發(fā)人員提供了快速構(gòu)建分布式系統(tǒng)中一些常見模式的工具(例如配置管理,服務(wù)發(fā)現(xiàn),斷路器,智...
    卡卡羅2017閱讀 136,569評論 19 139
  • SPRING CONFIG (ConfigFileApplicationListener) spring.conf...
    JeGe閱讀 6,813評論 0 14
  • 1. Java基礎(chǔ)部分 基礎(chǔ)部分的順序:基本語法,類相關(guān)的語法,內(nèi)部類的語法,繼承相關(guān)的語法,異常的語法,線程的語...
    子非魚_t_閱讀 34,728評論 18 399
  • 系列 iOS音視頻開源框架WebRTC入門-編譯(前序-授人魚不如授人以漁)iOS音視頻開源框架WebRTC入門-...
    tjfeng88閱讀 4,087評論 16 13
  • # 一級標(biāo)題 ## 二級標(biāo)題 ### 三級標(biāo)題 #### 四級標(biāo)題 ##### 五級標(biāo)題 ###### 六級標(biāo)題 ...
    張勇輝閱讀 169評論 0 0

友情鏈接更多精彩內(nèi)容