1.什么是Velocity
一种J2EE的前端模版技术,和JSP,Freemarker差不多,都是用来展示网页内容的。和JSP不同的是velocity只能显示Action中的数据,不能处理数据。不能写java代码,但是可以使用Velocity标记。也就是说把显示代码和后端的JAVA代码分离开来,降低程序的耦合性
2.需要引入哪些Jar包
velocity-1.5.jar,velocity-1.6.2.jar,velocity-tools-2.0.jar,velocity-tools-generic-2.0.jar,velocity-tools-view-2.0.jar
3.编写模板.vm文件
<html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> </head> <body> <!-- 页面对变量赋值 --> #set( $current = "Velocity22") <!-- 输出 --> $current is great! <br/>
<!-- 输出后台context设置的参数 --> $name <br/> <!--String循环--> #foreach( $elem in $arrList) $elem</br> #end <!--对象循环--> #foreach( $elem in $userList) 名字:$elem.name 性别:$elem.sex 地址:$elem.address</br> #end </body> </html>
4.编写servelet文件
package com.babybus.sdteam.servelet;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.util.ArrayList;
import java.util.Properties;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.velocity.VelocityContext;
import org.apache.velocity.app.Velocity;
import org.apache.velocity.app.VelocityEngine;
import org.apache.velocity.exception.MethodInvocationException;
import org.apache.velocity.exception.ParseErrorException;
import org.apache.velocity.exception.ResourceNotFoundException;
import com.babybus.sdteam.vo.User;
public class VelocityTemplateServelt extends HttpServlet {
private static final long serialVersionUID = 1L;
@Override
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws IOException, ServletException {
// 设置request 和 response 编码,放置乱码
request.setCharacterEncoding("UTF-8");
response.setContentType("text/html;charset=UTF-8");
// 获取页面输出流
PrintWriter out = response.getWriter();
// 创建Properties文件,也可以直接在目录下创建
Properties properties=new Properties();
properties.setProperty("resource.loader", "class");
properties.setProperty("class.resource.loader.class", "org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader");
properties.setProperty(Velocity.ENCODING_DEFAULT, "UTF-8");
properties.setProperty(Velocity.INPUT_ENCODING, "UTF-8");
properties.setProperty(Velocity.OUTPUT_ENCODING, "UTF-8");
// 创建模板引擎
VelocityEngine velocityEngine = null;
try {
velocityEngine = new VelocityEngine(properties);
} catch (Exception e) {
e.printStackTrace();
}
// 创建上下文, 用于存放变量
VelocityContext context=new VelocityContext();
context.put("name", "test");
// List(String)
ArrayList<String> arrList = new ArrayList<String>();
arrList.add("test01");
arrList.add("test02");
arrList.add("test03");
context.put("arrList", arrList);
// UserList(存放对象List)
ArrayList<User> userList = new ArrayList<User>();
userList.add(new User("蔡大三", "男", "南安一中五条巷子"));
userList.add(new User("马大哈", "男", "红灯区"));
userList.add(new User("林超", "女", "下三路"));
context.put("userList", userList);
// 读取模板文件流
StringWriter sw = new StringWriter();
try {
velocityEngine.mergeTemplate("templates/example.vm", "utf-8", context, sw);
} catch (ResourceNotFoundException e) {
e.printStackTrace();
} catch (ParseErrorException e) {
e.printStackTrace();
} catch (MethodInvocationException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
// 输出到页面
out.println(sw.toString());
}
}
5.配置web.xml
<?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">
<display-name></display-name>
<!--MyVelocity-->
<servlet>
<servlet-name>ve</servlet-name>
<servlet-class>com.babybus.sdteam.servelet.VelocityTemplateServelt</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>ve</servlet-name>
<url-pattern>/ve</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
6.输入地址:http://localhost:8080/velocitydemo/ve启动项目


本站文章为 宝宝巴士 SD.Team 原创,转载务必在明显处注明:(作者官方网站: 宝宝巴士 )
转载自【宝宝巴士SuperDo团队】 原文链接: http://www.cnblogs.com/superdo/p/4827097.html