二、Struts2和Spring2.5的整合
(1)在web.xml中加入Struts2过滤器
<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>
|
(2)导入struts2.1.6的jar包
这个里要导入7个jar包,可能很多教程上说只要加如5个核心包就可以了,但是2.1.6却要另外导入commons-io-1.3.2.jar、commons-fileupload-1.2.1.jar两个包,而且这个说明在官网的doc中也没有说明,这是我们需要注意一点的。当然我们也可以把struts2的所有包导入。
(3)加入struts2和spring整合的jar包
将struts2-spring-plugin-2.1.6.jar导入项目中。
(4)在src下建一个struts.xml,默认格式如下:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
</struts>
|
在<struts></struts>之间添加一个常量,将struts交给spring管理。
<struts>
<constant name="struts.objectFactory" value="spring"/>
</struts>
|
(5)修改web.xml文件,配置Spring监听器和上下文变量
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext*.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
|
注意:此配置需要放在过滤器之前。
增加OpenSessionInView过滤器的设置,放在struts过滤器之前
作用:
Spring中对OpenSessionInViewFilter的描述:它是一个Servlet2.3过滤器,用来把一个Hibernate Session和一次完整的请求过程对应的线程相绑定。目的是为了实现"Open Session in View"的模式。例如: 它允许在事务提交之后延迟加载显示所需要的对象。
|
<filter>
<filter-name>lazyLoadingFilter</filter-name>
<filter-class>org.springframework.orm.hibernate3.support.OpenSessionInViewFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>lazyLoadingFiter</filter-name>
<url-pattern>*.action</url-pattern>
</filter-mapping>
|
(6)编写Action类继承ActionSupport
package cn.zhang.crm.action;
import java.util.List;
import cn.zhang.crm.model.pojo.Employee;
import cn.zhang.crm.service.EmployeeManager;
import com.opensymphony.xwork2.ActionSupport;
publicclass EmployeeAction extends ActionSupport {
privatestaticfinallongserialVersionUID = -3117990268615208697L;
private EmployeeManager employeeManager;
private Employee employee;
private List employees;
privateintid;
publicint getId() {
returnid;
}
publicvoid setId(int id) {
this.id = id;
}
publicvoid setEmployeeManager(EmployeeManager employeeManager) {
this.employeeManager = employeeManager;
}
public String add(){
employeeManager.addEmployee(employee);
returnSUCCESS;
}
public String list(){
this.employees = employeeManager.listEmployee();
returnSUCCESS;
}
public String delete(){
employeeManager.deleteEmployee(id);
returnSUCCESS;
}
public List getEmployees() {
returnemployees;
}
publicvoid setEmployees(List employees) {
this.employees = employees;
}
publicvoid setEmployee(Employee employee) {
this.employee = employee;
}
public Employee getEmployee() {
returnemployee;
}
}
|
(7)配置struts.xml,将action写入
<package name="crm_employee" extends="struts-default" namespace="/emp">
<action name="add" class="addBean" method="add">
<result type="chain">
<param name="actionName">list</param>
</result>
</action>
<action name="list" class="listBean" method="list">
<result>list.jsp</result>
</action>
<action name="delete" class="deleteBean" method="delete">
<result>list.action</result>
</action>
<!-- Add actions here -->
</package>
|
(8)在applicationContext.xml中增加bean映射
<bean id="addBean" class="cn.zhang.crm.action.EmployeeAction" scope="prototype">
<property name="employeeManager">
<ref bean="employeeManager" />
</property>
</bean>
<bean id="listBean" class="cn.zhang.crm.action.EmployeeAction" scope="prototype">
<property name="employeeManager">
<ref bean="employeeManager" />
</property>
</bean>
<bean id="deleteBean" class="cn.zhang.crm.action.EmployeeAction" scope="prototype">
<property name="employeeManager">
<ref bean="employeeManager" />
</property>
</bean>
|
同时增加事务的处理
<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory">
<ref local="sessionFactory"/>
</property>
</bean>
<!-- 配置事务特性,处理add、delete、update等开始方法,传播特性为REQUIRED-->
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="add*" propagation="REQUIRED" />
<tx:method name="delete*" propagation="REQUIRED" />
<tx:method name="update*" propagation="REQUIRED" />
<tx:method name="list*" propagation="REQUIRED" />
<tx:method name="*" read-only="true" />
</tx:attributes>
</tx:advice>
<aop:config>
<aop:pointcut id="allManagerMethod"
expression="execution(* cn.zhang.crm.service.*.*(..))" />
<aop:advisor advice-ref="txAdvice"
pointcut-ref="allManagerMethod" />
</aop:config>
|
要让applicationContext.xml认得事务处理,我们需要将beans头识别修改一下,如下:
<beans
xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-2.5.xsd ">
|
(9)添加jsp文件
emp/add.jsp
<%@ page language="java" pageEncoding="UTF-8"%>
<%@ taglib uri="/struts-tags" prefix="s"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>雇员用户信息增加</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<h1>雇员用户信息输入</h1>
<s:form action="add">
<s:textfield name="employee.name" label="姓名" />
<s:textfield name="employee.address" label="地址"/>
<s:textfield name="employee.phone" label="电话" />
<s:submit/>
</s:form>
</body>
</html>
|
emp/list.jsp
<%@ page language="java" pageEncoding="UTF-8"%>
<%@ taglib uri="/struts-tags" prefix="s"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>雇员用户信息显示</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<h1>雇员用户信息显示</h1>
<table>
<tr>
<td>编号</td>
<td>姓名</td>
<td>地址</td>
<td>电话</td>
</tr>
<s:iterator value="employees">
<tr>
<td><s:property value="id"/></td>
<td><s:property value="name"/></td>
<td><s:property value="address"/></td>
<td><s:property value="phone"/></td>
</tr>
</s:iterator>
</table>
</body>
</html>
|
(10)部署,测试效果
http://localhost:8088/crm/emp/add.jsp
到这里,整个实例算运行起来了。至于其他的几个操作,如修改、删除就自己做了。