ve locity使用1.7版本。 在win7下使用intelliJ IDEA建立一基于tomcat的web app项目,命名为todo_web,设置path为/todo,导入velocity相关jar包。只导入velocity-1.7.jar这个包可能会报错,根据提示再导入velocity自带的其他包。 项目结构如下:
测试Tomcat
index.jsp内容如下:
HelloWorld.java内容如下:
import java.io.*; import javax.servlet.*; import javax.servlet.http.*; public class HelloWorld extends HttpServlet { /** * * @param request * @param response * @throws IOException * @throws ServletException */ @Override public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException { response.setContentType("text/html"); PrintWriter out = response.getWriter(); out.println(""); out.println(""); out.println("Hi! "); out.println(""); out.println(""); out.println("Hello World!!!
"); out.println(""); out.println(""); } }
在web.xml中加入以下内容:
hi HelloWorld hi /hi
运行项目,在http://localhost:8080/todo和http://localhost:8080/todo/hi中可以看到效果。
使用velocity
下面开始使用velocity模板引擎,在src下建立目录templates,在templates目录下建立文件test.vm,内容如下:
#set( $this = "Velocity") $this is great!
$name
hi , i am letian你好
在src目录下新建java文件MyVelocity01.java:
import java.io.*; import javax.servlet.*; import javax.servlet.http.*; import org.apache.velocity.app.Velocity; import org.apache.velocity.app.VelocityEngine; import org.apache.velocity.VelocityContext; import java.util.Properties; public class MyVelocity01 extends HttpServlet { @Override public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException { request.setCharacterEncoding("UTF-8"); response.setContentType("text/html;charset=UTF-8"); PrintWriter out = response.getWriter(); Properties properties=new Properties(); properties.setProperty("resource.loader", "class"); properties.setProperty("class.resource.loader.class", "org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader"); //properties.setProperty("input.encoding", "UTF-8"); //properties.setProperty("output.encoding", "UTF-8"); properties.setProperty(Velocity.ENCODING_DEFAULT, "UTF-8"); properties.setProperty(Velocity.INPUT_ENCODING, "UTF-8"); properties.setProperty(Velocity.OUTPUT_ENCODING, "UTF-8"); VelocityEngine velocityEngine = new VelocityEngine(properties); VelocityContext context=new VelocityContext(); context.put("name", "test"); StringWriter sw = new StringWriter(); velocityEngine.mergeTemplate("templates/test.vm", "utf-8", context, sw); //velocityEngine.mergeTemplate("templates/test.vm", "utf-8", context, sw); //这样就会出现两次 out.println(sw.toString()); } }
配置web.xml:
ve MyVelocity01 ve /ve
重新部署,浏览器访问http://localhost:8080/todo/ve可以看到效果。
简单介绍velocity
velocity是一个基于java的模板引擎,有三种文件加载模板方式: 1、从文件路径加载 2、从类路径(MyVelocity01.java使用该方法) 3、从jar文件加载 开始接触velocity时可能会在加载模板上遇到问题。
如何向模板文件传递变量: 模板本身可以定义变量,例如在test.vm中定义了变量$this,java代码也可以给模板传递变量,例如test.vm中的变量$name便是VelocityContext实例传递过去的。同时velocity也支持迭代对象,例如: 我们在MyVelocity01.java中导入java.util.Vector,将代码:
context.put("name", "test");
改为:
Vector v = new Vector(); v.addElement("Harry"); v.addElement("John"); String[] names = {"Harry", "John"}; context.put("names1", v); context.put("names2", names);
将test.vm内容改为:
hello
#foreach($name in $names1) $name
#end #foreach($name in $names2) $name
#end
velocity还支持map容器,支持使用#include("")引入静态模板,#parse("模板名")引入动态模板。
如果想不开要用java MVC写网站的话,使用servlet velocity是一个小巧灵活的选择。