sttuts2和velocity整合

 在昨天是使用velocity和Servlet整合,其中使用继承(extends) VelocityViewServlet来实现servlet解析vm模板,发现这样写比较麻烦,今天研究了一下怎么和struts2进行整合,发现struts2可以很好的整合velocity。

 

项目结构为:


sttuts2和velocity整合
 

这个项目是用maven搭的项目,因为发现找jar包太麻烦了。

 

其中pom.xml文件为:

写道
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>VelocityStruts</groupId>
<artifactId>VelocityStruts</artifactId>
<packaging>war</packaging>
<name />
<version>0.0.1-SNAPSHOT</version>
<description />
<build>
<sourceDirectory>${basedir}/src</sourceDirectory>
<outputDirectory>${basedir}/WebRoot/WEB-INF/classes</outputDirectory>
<resources>
<resource>
<directory>${basedir}/src</directory>
<excludes>
<exclude>**/*.java</exclude>
</excludes>
</resource>
</resources>
<plugins>
<plugin>
<artifactId>maven-war-plugin</artifactId>
<configuration>
<webappDirectory>${basedir}/WebRoot</webappDirectory>
<warSourceDirectory>${basedir}/WebRoot</warSourceDirectory>
</configuration>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.5</source>
<target>1.5</target>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>org.apache.openejb</groupId>
<artifactId>javaee-api</artifactId>
<version>5.0-1</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.faces</groupId>
<artifactId>jsf-api</artifactId>
<version>1.2_04</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>jsp-api</artifactId>
<version>2.1</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.faces</groupId>
<artifactId>jsf-impl</artifactId>
<version>1.2_04</version>
<scope>provided</scope>
</dependency>

<dependency>
<groupId>org.apache.struts</groupId>
<artifactId>struts2-core</artifactId>
<version>2.2.1</version>
</dependency>


<dependency>
<groupId>org.apache.velocity</groupId>
<artifactId>velocity-tools</artifactId>
<version>2.0</version>
</dependency>

<dependency>
<groupId>org.apache.velocity</groupId>
<artifactId>velocity</artifactId>
<version>1.7</version>
</dependency>
<dependency>
<groupId>javassist</groupId>
<artifactId>javassist</artifactId>
<version>3.8.0.GA</version>
</dependency>

<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.12</version>
</dependency>

<dependency>
<groupId>avalon-logkit</groupId>
<artifactId>avalon-logkit</artifactId>
<version>2.1</version>
</dependency>
<dependency>
<groupId>commons-collections</groupId>
<artifactId>commons-collections</artifactId>
<version>3.2.1</version>
</dependency>

</dependencies>
</project>

 

在用servlet和velocity结合的时候,需要手动的覆盖handleRequest(HttpServletRequest request,
            HttpServletResponse response, Context ctx)方法,并且手动的往ctx中写数据,才能在vm模板中读到数据,但是用struts2中,已经完全的简化了该步骤,直接像写普通的action一样,代码如下:

写道
package com.velocity.action;

import java.util.ArrayList;
import java.util.List;

import com.velocity.bean.Department;
import com.velocity.bean.Employee;

public class HelloWorld {

private String message;
private String userName;
private List<Employee> list=new ArrayList<Employee>();

public List<Employee> getList() {
return list;
}

public void setList(List<Employee> list) {
this.list = list;
}

public HelloWorld() {
}

public String execute() {
setMessage("Hello " + getUserName());


list.add(new Employee(1,"张三","北京",18,new Department(1,"软件研发部1")));
list.add(new Employee(2,"张三","北京",19,new Department(2,"软件研发部2")));
list.add(new Employee(3,"张三","北京",20,new Department(3,"软件研发部3")));
list.add(new Employee(4,"张三","北京",21,new Department(4,"软件研发部4")));
list.add(new Employee(5,"张三","北京",22,new Department(5,"软件研发部5")));
list.add(new Employee(6,"张三","北京",23,new Department(6,"软件研发部6")));
list.add(new Employee(7,"张三","北京",24,new Department(7,"软件研发部7")));
list.add(new Employee(8,"张三","北京",25,new Department(8,"软件研发部8")));
list.add(new Employee(9,"张三","北京",26,new Department(9,"软件研发部9")));
list.add(new Employee(10,"张三","北京",27,new Department(10,"软件研发部10")));


return "SUCCESS";
}

public String getMessage() {
return message;
}

public void setMessage(String message) {
this.message = message;
}

public String getUserName() {
return userName;
}

public void setUserName(String userName) {
this.userName = userName;
}

}

 其中的bean类和上一篇文章中的一模一样,在这里就不重复了。

 

在struts中需要添加一个配置文件tools.xml(也可以不需要,该xml文件,就如他的名字一样,可以在vm中直接使用该配置文件中添加的工具变量,如date就是一个工具变量,可以在vm中通过日期:$date.get('yyyy-M-d H:m:s') 使用),代码如下:

写道
<?xml version="1.0"?>
<toolbox>
 <tool>
  <key>date</key>
  <scope>application</scope>
  <class>org.apache.velocity.tools.generic.DateTool</class>
  <parameter name="format" value="yyyy-M-d" />
 </tool>
 <tool>
  <key>link</key>
  <scope>request</scope>
  <class>org.apache.velocity.tools.struts.StrutsLinkTool</class>
 </tool>
 <tool>
  <key>msg</key>
  <scope>request</scope>
  <class>org.apache.velocity.tools.struts.MessageTool</class>
 </tool>
 <tool>
  <key>errors</key>
  <scope>request</scope>
  <class>org.apache.velocity.tools.struts.ErrorsTool</class>
 </tool>
 <tool>
  <key>form</key>
  <scope>request</scope>
  <class>org.apache.velocity.tools.struts.FormTool</class>
 </tool>
 <tool>
  <key>tiles</key>
  <scope>request</scope>
  <class>org.apache.velocity.tools.struts.TilesTool</class>
 </tool>
 <tool>
  <key>validator</key>
  <scope>request</scope>
  <class>org.apache.velocity.tools.struts.ValidatorTool</class>
 </tool>
</toolbox>

 如果想使用toolbox,必须在web.xml中添加如下一段话:

<init-param>
<param-name>org.apache.velocity.toolbox</param-name>
<param-value>/WEB-INF/toolbox.xml</param-value>     
</init-param>

 

 

 

以后的代码就和servlet类似了,在该项目中,我把vm模板文件放在了项目的根目录中了,如果想找到他的话,需要配置velocity.properties文件

写道
resource.loader = webapp
webapp.resource.loader.class = org.apache.velocity.tools.view.servlet.WebappLoader
webapp.resource.loader.path=  ---这里是配置vm的路径的
input.encoding=utf-8
output.encoding=utf-8

runtime.log =d/\:/velocity.log

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">
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>

<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>
</servlet>

<!-- Map *.vm files to Velocity -->
<servlet-mapping>
<servlet-name>velocity</servlet-name>
<url-pattern>*.vm</url-pattern>
</servlet-mapping>

<welcome-file-list>
<welcome-file>index.vm </welcome-file>
</welcome-file-list>


</web-app>

 

 

在web项目中loader.class 需要用 org.apache.velocity.tools.view.servlet.WebappLoader
其他的项目直接使用velocity包中的类就可以了。

velocity中也支持struts2中的ValueStack的数据,这样就轻松了很多,哈哈

其他的就没有什么可以说了,反正觉得struts和velocity结合的非常好。上传代码……

你可能感兴趣的:(velocity)