http://llying.iteye.com/blog/419685
用veloctiy代替jsp作为程序视图展示
这里我们需要一个velocity的扩展工具包veloctiy-tools,我们可以在velocity的官方网站中得到。
在web.xml中配置相关的servlet
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<servlet>
<servlet-name>velocity</servlet-name>
<servlet-class>
org.apache.velocity.tools.view.servlet.VelocityViewServlet
</servlet-class>
<init-param>
<param-name>org.apache.velocity.toolbox</param-name>
<param-value>/WEB-INF/toolbox.xml</param-value>
</init-param>
<init-param>
<param-name>org.apache.velocity.properties</param-name>
<param-value>/WEB-INF/velocity.properties</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>velocity</servlet-name>
<url-pattern>*.vm</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
toolbox.xml是实体映射文件后面会有说明
velocity.properties是配置文件
这里仅设置了字符编码
input.encoding=utf-8
output.encoding=utf-8
directive.foreach.counter.name = velocityCount
directive.foreach.counter.initial.value = 1
velocimacro.library = /WEB-INF/conf/vm/VM_global_library.vm,/WEB-INF/conf/vm/anyplaceMacro.vm
velocimacro.permissions.allow.inline = true
velocimacro.permissions.allow.inline.to.replace.global = false
velocimacro.permissions.allow.inline.local.scope = false
velocimacro.context.localscope = false
这里我们创建了一个默认的vm模板文件
VM_global_library.vm 放置在/WEB-INF/conf/vm/根目录下 放置一些全局的数据
接下来我们创建一个Person类
public class Person {
private String name;
private int id;
private int age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
PersonService实际处理的对象
import java.util.ArrayList;
import java.util.List;
public class PersonService {
static private List<Person> PERSONS = new ArrayList<Person>();
static{
for (int i = 0 ; i<10;i++){
Person p = new Person();
p.setId(i);
p.setAge(i+3);
p.setName("p_name "+i);
PERSONS.add(p);
}
}
public List<Person> getPersons(){
return PERSONS;
}
public Person getPerson(int id){
for(int i = 0 ; i<PERSONS.size() ; i++){
Person p = PERSONS.get(i);
if(id==p.getId()){
return p;
}
}
return null;
}
public int parseInt(String id){
return Integer.parseInt(id);
}
}
现在再看看我们的映射文件应该可以很明确了
<?xml version="1.0" encoding="UTF-8"?>
<toolbox>
<data type="string">
<key>personList</key>
<value>用户列表</value>
</data>
<tool>
<key>personService</key>
<scope>request</scope>
<request-path>/person/*</request-path>
<class>PersonService</class>
</tool>
</toolbox>
下面看看我们的两个模板
一个是显示全部 一个是显示单个
persons
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<TITLE> $personList </TITLE>
</HEAD>
<BODY>
<table>
<tr>
<td>姓名</td>
<td>年龄</td>
</tr>
#foreach($person in ${personService.getPersons()})
<tr>
<td><a href="person.vm?id=${person.getId()}">${person.getName()}</a></td>
<td>${person.getAge()}</td>
<tr>
#end
</table>
</BODY>
</HTML>
person
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<TITLE> $personList </TITLE>
</HEAD>
<BODY>
#set ($paramName = $request.getParameter("id"))
$paramName
<table>
<tr>
<td>姓名</td>
<td>年龄</td>
</tr>
#set( $person = ${personService.getPerson($personService.parseInt($paramName))} )
<tr>
<td>${person.getName()}</td>
<td>${person.getAge()}</td>
<tr>
</table>
</BODY>
</HTML>
接下来大家可以看看效果 不过很简陋
有一点令我十分不爽 就是String --> int转换 我自己写得方法
如果有知道更好的希望赐教