Struts2+Spring2.5.6+Hibernate3+Freemarker整合(二)继上一篇文章继续接下来的配置,上次已经将spring与hibernate的整合做了一个简单的配置,现在开始融入struts2,struts2采用零配置,所以必须得首先配置struts2.xml。
- <?xml version=”1.0″ encoding=”UTF-8″ ?>
- <!DOCTYPE struts PUBLIC ”-//Apache Software Foundation//DTD Struts Configuration 2.1//EN” ”http://struts.apache.org/dtds/struts-2.1.dtd”>
- <struts>
- <!– 国际化编码 –>
- <constant name=”struts.i18n.encoding” value=”UTF-8″ />
- <!– 定位视图资源的根路径。默认值为/WEB-INF/content –>
- <constant value=”/WEB-INF/templates” name=”struts.convention.result.path” />
- <!– 指定convention扫描以xxx结尾的包 –>
- <constant value=”action” name=”struts.convention.package.locators” />
- <!– 是否将Action类转换成小写 –>
- <constant value=”false” name=”struts.convention.package.lowercase” />
- <!– 是否将actionName分割,去掉action部分,以大写字母作为分割 –>
- <constant value=”" name=”struts.convention.action.name.separator” />
- <!– 设置默认的父包 –>
- <constant value=”MAIN” name=”struts.convention.default.parent.package” />
- <package name=”MAIN” extends=”struts-default” namespace=”/”>
- </package>
- </struts>
struts.xml的一些零配置变量已经注册好,既然使用的spring,那当然得让spring来帮我们管理action咯,不然就失去s2sh整合的意义咯。下面我们来看看如何让struts2融入到这个框架中,首先得在lib包中加上struts2-convention-plugin-2.2.1.jar与struts2-spring-plugin-2.1.8.1.jar两个包,分别是零配置与spring插件。接下来配置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”>
- <listener>
- <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
- </listener>
- <context-param>
- <param-name>contextConfigLocation</param-name>
- <param-value>classpath*:applicationContext*.xml</param-value>
- </context-param>
- <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>
- </web-app>
配置文件已经写好了,下面来写一个Action类
- /**
- *
- */
- package com.action;
- import java.util.List;
- import javax.annotation.Resource;
- import org.apache.log4j.Logger;
- import org.apache.struts2.convention.annotation.Action;
- import org.apache.struts2.convention.annotation.ParentPackage;
- import org.apache.struts2.convention.annotation.Result;
- import org.apache.struts2.convention.annotation.Results;
- import org.springframework.context.annotation.Scope;
- import org.springframework.stereotype.Component;
- import com.opensymphony.xwork2.ActionSupport;
- import com.service.HibernateTemplateService;
- /**
- *
- * @author 张廷 2011-3-19下午07:36:40
- *
- */
- @ParentPackage(“MAIN”)
- @Action(“RegistAction”)
- @Results( { @Result(name = ”list”, location = ”list.ftl”, type = ”freemarker”) })
- @Component(“RegistAction”)
- @Scope(“prototype”)
- public class RegistAction extends ActionSupport {
- private static final long serialVersionUID = 773060530483225473L;
- @Resource(name = ”hibernateTemplateServiceImpl”)
- private HibernateTemplateService hibernateTemplateService;
- private Student student;
- private Logger logger = Logger.getLogger(this.getClass());
- private List<Student> stuList;
- @SuppressWarnings(“unchecked”)
- public String findStu() {
- stuList = hibernateTemplateService.findAllStu(“from Student”);
- return ”list”;
- }
- public List<Student> getStuList() {
- return stuList;
- }
- public void setStuList(List<Student> stuList) {
- this.stuList = stuList;
- }
- public HibernateTemplateService getHibernateTemplateService() {
- return hibernateTemplateService;
- }
- public void setHibernateTemplateService(HibernateTemplateService hibernateTemplateService) {
- this.hibernateTemplateService = hibernateTemplateService;
- }
- }
一个简单Action类已经写好了,在这说明一下其中的注释,@ParentPackage是指明继承于哪个包,@Action注册一个action组件,在注册action中的name一定要和@Component的组件名相同,不然Spring是找不到相应的action与之对应的。因为struts2本身就支持freemarker,而且freemarker是struts2的依赖包,固然struts2支持freemarker的返回类型,所以在Result中我们将返回的类型设置为freemarker,视图则指定为相应的ftl文件。万事具备,只欠freemarker模板了,下面来看看如何写一个freemarker。在WEB-INF下创建一个templates文件夹,在该文件夹目录下创建list.ftl文件。
- <!DOCTYPE html PUBLIC ”-//W3C//DTD XHTML 1.0 Transitional//EN” ”http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd”>
- <html xmlns=”http://www.w3.org/1999/xhtml”>
- <head>
- <meta http-equiv=”Content-Type” content=”text/html; charset=utf-8″ />
- <title>list display</title>
- </head>
- <body>
- <#–当前时间:${date}–>
- <table border=1>
- <tr>
- <th>学号</th>
- <th>姓名</th>
- <th>年龄</th>
- <th>生日</th>
- <th>性别</th>
- </tr>
- <#list stuList as stu>
- <tr>
- <td>${stu.stuNumber}</td>
- <td>${stu.stuName}</td>
- <td>${stu.age}</td>
- <td>${stu.birth}</td>
- <td>${stu.sex.sexName}</td>
- </tr>
- </#list>
- </table>
- </body>
- </html>
这只是一个简单的freemarker模版,这不做深入讲解。当然还要配置相应的freemarker.properties文件,不然出现乱码哦。
freemarker.properties
- template_update_delay=1
- datetime_format=yyyy-MM-dd HH:mm:ss
- date_format=yyyy-MM-dd
- time_format=HH:mm:ss
- number_format=0.######;
- boolean_format=true,false
- whitespace_stripping=true
- locale=zh_CN
- default_encoding=UTF-8
- url_escaping_charset=UTF-8
- classic_compatible=true
一切都已就绪,访问一下试试看吧,输入http://localhost:8080/S2SH_FreeMarker/RegistAction!findStu,看一下结果吧。

结果是已经成功了,s2sh+freemarker的整合就已经完成了。
http://z276356445t.iteye.com/blog/977695