苹果笔记本优酷怎么看:[velocity]Struts2下配置Velocity1.6ss

来源:百度文库 编辑:九乡新闻网 时间:2024/04/30 04:17:09
先说一下环境吧。
Myeclipse6.5+Struts2.1.8+Velocity1.6.4。
在eclipse中创建一个web project。在/WEB-INF/lib/中添加Struts2和velocity所需要的jar包。虽然struts2中已经自带了velocity的jar包,但版本不是最新的。所以从apache官方网站的maven库中下载比较新的版本。
velocity需要的jar包如下:
velocity-1.6.4.jar
velocity-tools-1.4.jar
commons-collections-3.2.jar
commons-lang-2.3.jar
然后编辑web.xml
   
       velocitylayout
       org.apache.velocity.tools.view.servlet.VelocityViewServlet
   

   
       velocitylayout
       *.vm
   

在web.xml中添加对vm文件的处理。
然后在struts.xml中添加测试使用的action。
   
       
            /hello.vm
       

   

随后在/WebRoot/下创建hello.vm

 
    Velocity
   
 
 
   

你好: $name


 

然后是IndexAction。
public class IndexAction extends ActionSupport {
   
    private String name;
   
    public String getName() {
        return name;
    }
   
    public void setName(String name) {
        this.name = name;
    }
   
    public String execute() {
        name = "Hello, " + name + "!";
        return SUCCESS;
    }
}
最后在index.jsp中
<%@ page language="java" pageEncoding="UTF-8" contentType="text/html;charset=utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>



 
   
    这个是测试Velocity的程序
   
   
       
   
   
   
 
 
 
     

         
         
     

 

提交action处理后返回hello.vm中。
最后在hello.vm中:

 
    Velocity
   
 
 
   

Message rendered in Velocity: $name