1. Velocity 開(kāi)發(fā)
apache Velocity開(kāi)發(fā)指導(dǎo)傳送門
1.1 Velocity使用流程
- 初始化Velocity(單例或者多實(shí)例)
- 創(chuàng)建一個(gè)context
- 向context中添加對(duì)象
- 選擇模版
- 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對(duì)象
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垒探。沒(méi)有特殊需求的話可以使用官方推薦的VelocityContext纳猪,不支持jdk對(duì)象序列化怕吴。
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 為空新建一個(gè)
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接口的對(duì)象
- 數(shù)組會(huì)被velocity包裝成實(shí)現(xiàn)Iterable的類,v1.6之后可以把數(shù)組當(dāng)成list來(lái)看待喻奥,可以使用size(), isEmpty() and get(int)方法汉买。
- Map values()方法需要返回有用的信息
- Enumeration只能使用一次,沒(méi)有復(fù)位會(huì)報(bào)錯(cuò)
### 1.4.2.2 支持static類
不是所有的類都可以創(chuàng)建對(duì)象邻悬,比如Math類沒(méi)有公有的構(gòu)造器症昏,要用到Math的方法可以直接把math的class對(duì)象傳過(guò)去。
```
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í)際上存的是最后一個(gè)key的值,上面代碼中g(shù)et duplicate會(huì)得到I am in context2
- 重復(fù)的key不會(huì)影響原有的context中的值拘悦,context1.get("duplicate")獲取到的還是I am in context1
- #set()可以影響到外層的context的值,但是不影響內(nèi)層的