Velocity2.0版本后velocity+servlet
Velocity2.0版本之后,用VelocityViewServlet代替了原来的VelocityServlet,通过源代码分析,可以知道新的VelocityViewServlet是如何工作的。
通过下面的说明,仅提出Velocity在Web方面的简单示例,为大家献上一个原始但清晰的认识,来了解Velocity在Web方面的工作原理,未来还有深入的主题贡献给大家。
使用服务器环境是Tomcat7.0
首先我们还是使用VTL(Velocity Template Language)编一个.vm模版,考察在网页设计师的角度是不是很容易理解和编辑,首先创建sample.vm内容如下:
1 < html > 2 < head >< title > Velocity </ title ></ head > 3 < body bgcolor = " #ffffff " > 4 < center > 5 < h2 > Welcom to Velocity !</ h2 > 6 < i > Here ' s the list of people</i> 7 < table cellspacing = " 0 " cellpadding = " 5 " width = " 20% " > 8 < tr > 9 < td bgcolor = " #eeeeee " align = " center " > 10 Names: 11 </ td > 12 </ tr > 13 #foreach ($name in $theList) 14 < tr > 15 < td bgcolor = " #eeeeee " align = " center " > $name </ td > 16 </ tr > 17 #end 18 </ table > 19 </ center > 20 </ body > 21 </ html >
接下来看看最简单的Servlet是怎么和Velocity结合工作的,创建SampleServlet.java,由于VelocityViewServlet提供了统一的Servlet入口和封装了大部分工作,以及把展示数据合并到模版中,所以SampleServlet通过继承VelocityViewServlet,j将变的比VelocityServlet更加简单,只需要实现
public Template handleRequest( HttpServletRequest request,HttpServletResponse response, Context ctx)方法,代码如下:
1 package com; 2 3 import java.io.FileNotFoundException; 4 import java.io.IOException; 5 import java.util.Properties; 6 import java.util.Vector; 7 8 import javax.servlet.ServletConfig; 9 import javax.servlet.http.HttpServletRequest; 10 import javax.servlet.http.HttpServletResponse; 11 12 import org.apache.velocity.Template; 13 import org.apache.velocity.app.Velocity; 14 import org.apache.velocity.context.Context; 15 import org.apache.velocity.exception.ParseErrorException; 16 import org.apache.velocity.exception.ResourceNotFoundException; 17 import org.apache.velocity.tools.view.VelocityView; 18 import org.apache.velocity.tools.view.VelocityViewServlet; 19 20 public class SampleServlet extends VelocityViewServlet { 21 private static final long serialVersionUID = -3329444102562079189L; 22 23 @Override 24 public Template handleRequest( HttpServletRequest request, 25 HttpServletResponse response, Context ctx ) { 26 27 // 主要在此设置演示用的数据,开发中在此调用相应的业务处理流程, 28 29 // 并设置返回到页面的数据 30 31 System.out.println( " ------SampleVelocity.handleRequest------- " ); 32 // 待展示的列表数据 33 String p1 = " 第一位:LiuDong " ; 34 String p2 = " 第二位:Liang.xf " ; 35 Vector<String> personList = new Vector<String>(); 36 // 中文需要转换 37 try { 38 personList.addElement( new String(p1.getBytes(),"utf-8")); 39 personList.addElement( new String(p2.getBytes(),"utf-8")); 40 } catch (Exception e) { 41 System.out.println( " 数据转换异常: " + e); 42 } 43 // 设置数据,供页面模版替换成显示的数据 44 ctx.put( "theList" , personList ); 45 /* 46 * get the template. There are three possible 47 * exceptions. Good to know what happened. 48 */ 49 Template outty = null ; 50 try { 51 outty = getTemplate( "Sample.vm" ); 52 } catch ( ParseErrorException pee ) { 53 System.out.println( " SampleServlet: parse error for template " + pee); 54 } catch ( ResourceNotFoundException rnfe ) { 55 System.out.println( " SampleServlet: template not found " + rnfe); 56 } catch ( Exception e ) { 57 System.out.println( " Error " + e); 58 } 59 return outty; 60 } 61 }
而且在新的类中,不需要硬编码的形式配置velocity,可以简单的使用properties文件,web.xml文件如下:
<servlet> <servlet-name > SampleServlet </servlet-name > <servlet-class > com.SampleServlet </servlet-class > <init-param> <param-name>org.apache.velocity.properties</param-name> <param-value>/WEB-INF/test.properties</param-value> </init-param> </servlet> <servlet-mapping > <servlet-name >SampleServlet </servlet-name > <url-pattern >/SampleServlet </url-pattern > </servlet-mapping >
test.properties如下:
resource.loader = webapp
webapp.resource.loader.class = org.apache.velocity.tools.view.servlet.WebappLoader
webapp.resource.loader.path=/
input.encoding="utf-8"
output.encoding="utf-8"
动Tomcat,在IE上输入:http://localhost:8080/test/SampleServlet,页面如下: