继
上一篇文章继续接下来的配置,上次已经将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的整合就已经完成了。
- 大小: 31.8 KB
分享到:
相关推荐
包含 Struts2.1.6 + Spring2.5.6 + Hibernate3.3.2 整合的包。
struts2+spring2.5.6+hibernate3.0+ext
Struts2.1.8+Spring2.5.6+hibernate3.2.3整合
SSH项目整合,包含本项目所有的JAR包,基于mysql数据库,完成了基本的“增,删,改,查,完整分页”,另外做了一点js验证,以及建表,插入指定数量的随机测试数据...... 绝对值这个分。
Struts2.1.6+Spring2.5.6+Hibernate3.3.1整合 步骤非常详细,每步都有说明!
Struts2.1.6+Spring2.5.6+Hibernate3.3.1全注解
struts2.1.8 + spring2.5.6 + hibernate3.3.2整合,可以看一下,比较完全
Struts2.1.6+Spring2.5.6+Hibernate3.3.1框架整合常见错误.txt 初学者开发常见错误!
Struts2.1.6 +Spring2.5.6 + Hibernate3.3.2整合包
struts2.1.8+spring2.5.6+hibernate3.3.2整合项目开发 比较完整哦 可以作为模板的
Struts2.1.6+Spring2.5.6+Hibernate3.3.1全注解实例详解 docx文档教程
自己整理的struts1.3.8+spring2.5.6+hibernate3.3框架集成,项目带jar包,直接导入即可运行。
最求效率 struts1+spring2.5.6+jdbc
Struts2+Spring2.5.6+Hibernate3 用到的jar包 antlr-2.7.6.jar asm.jar backport-util-concurrent.jar cglib-nodep-2.1_3.jar commons-collections-3.1.jar commons-dbcp.jar commons-fileupload-1.2.1.jar commons-...
本Demo基于annotation的Struts2.1.8+Spring2.5.6+Hibernate3.3.2的整合
Struts2.1.6+Spring2.5.6+Hibernate3.3.1框架整合开发 http://www.cnitblog.com/intrl/archive/2009/04/13/56322.aspx 这篇文章的源码由于他的上传的空间用户可能下载失败,作者相当牛而且具有奉献精神
Struts2.1.6+Spring2.5.6+Hibernate3.3.1全注解实例详解 参照大象的实例 亲测可以正常运行 给那些找JAR包纠结有朋友们
struts2.2.3+spring2.5.6+hibernate3.2+proxool0.9.1 proxool数据库连接池例子 0资源下载
Struts2.0.14+Spring2.5.6+Struts.1.3.8+Hibernate3.2,这里集合了struts,hibernate,Spring的核心包,都是核心的,重要文件