1. Velocity 開發
1.1 Velocity使用流程
- 初始化Velocity(單例或者多實例)
- 創建一個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
- 多實例模式使用: org.apache.velocity.app.VelocityEngine
1.3.1 單例模式使用velocity
//初始化Velocity
Velocity.init();
//創建context
VelocityContext context = new VelocityContext();
//添加數據
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 多實例使用velocity
//每次創建VelocityEngine對象
VelocityEngine ve = new VelocityEngine();
ve.setProperty( VelocityEngine.RUNTIME_LOG_LOGSYSTEM, this);
ve.init();
//創建context
VelocityContext context = new VelocityContext();
//添加數據
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是作為數據容器的,實現是基于HashMap的,模版中用到的變量都必須放到context中,類似于spring中的ModelAndView。沒有特殊需求的話可以使用官方推薦的VelocityContext,不支持jdk對象序列化。
VelocityContext構造器默認實現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( ) 支持的數據結構
- 實現Iterable接口的對象
- 數組會被velocity包裝成實現Iterable的類,v1.6之后可以把數組當成list來看待,可以使用size(), isEmpty() and get(int)方法。
- Map values()方法需要返回有用的信息
- Enumeration只能使用一次,沒有復位會報錯
### 1.4.2.2 支持static類
不是所有的類都可以創建對象,比如Math類沒有公有的構造器,要用到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 );
```
- 如果有重復的key最后實際上存的是最后一個key的值,上面代碼中get duplicate會得到I am in context2
- 重復的key不會影響原有的context中的值,context1.get("duplicate")獲取到的還是I am in context1
- #set()可以影響到外層的context的值,但是不影響內層的