SSH(二)
SSH框架整合的系统架构,Action、Service、Dao、SessionFactory、DataSource都可以作为Spring的Bean组件管理
使用HibernateDaoSupport基类(二):
1.实现步骤:
1).DAO类继承HibernateDaoSupport
2).使用getHibernateTemplate()方法获取HibernateTemplate实例完成持久化操作
简化Hibernate DAO的编码
2.将SessionFactory注入DAO -- HibernateDaoSupport基类的setSessionFactory()方法
范例:
1 <bean id="userDao" class="com.xuetang9.demo.dao.impl.UserDaoImpl"> 2 <property name="sessionFactory" ref="sessionFactory"></property> 3 </bean>
3.创建HibernateTemplate实例
添加业务层和事务机制:
1.控制事务
a.可以采用Hibernate控制事务
b.事务应该在业务逻辑层控制
c.硬编码方式,代码繁琐,且破坏分层,代码不易维护
所以:
1.可以采用AOP的方式实现
2.Spring提供了声明式事务支持
2.配置声明式事务
1)核心问题:对不同的方法,采取不同的事务策略
2)步骤:
a.导入tx和aop命名空间
b.配置事务管理器,并为其注入SessionFactory
c.基于该事务管理器配置事务增强,指定事务规则
d.定义切入点
f.织入事务切面
范例:
1 <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager"> 2 <property name="sessionFactory" ref="sessionFactory"></property> 3 </bean> 4 <tx:advice id="tx" transaction-manager="transactionManager"> 5 <tx:attributes> 6 <tx:method name="search*" propagation="REQUIRED" read-only="true"/> 7 <tx:method name="modify*" propagation="REQUIRED"/> 8 </tx:attributes> 9 </tx:advice> 10 11 <aop:config> 12 <aop:pointcut expression="execution(* com.xuetang9.demo.service..*.*(..) )" id="txpoint"/> 13 <aop:advisor advice-ref="tx" pointcut-ref="txpoint"/> 14 </aop:config>
3)事务属性
1.tx:method
a.propagation:事务传播机制
REQUIRED(默认值)
SUPPORTS
b.isolation:事务隔离等级
DEFAULT(默认值)
READ_COMMITTED
c.timeout:事务超时时间。
允许事务运行的最长时间,以秒为单位。默认值为-1,表示不超时
d.read-only:事务是否为只读。
默认值为false
e.rollback-for:设定能够触发回滚的异常类型
Spring默认只在抛出runtime exception时才标识事务回滚
可以通过全限定类名指定需要回滚事务的异常,多个类名用逗号隔开
f.no-rollback-for:设定不触发回滚的异常类型
Spring默认checked Exception不会触发事务回滚
可以通过全限定类名指定不需回滚事务的异常,多个类名用英文逗号隔开
Spring与Struts2集成:
1.实现步骤:
1)添加struts2-spring-plugin-xxx.jar
2)按照名称匹配的原则定义业务Bean和Action中的setter方法
3)在struts.xml正常配置Action
2.模板与回调机制
Spring提供回调机制:
a.模板固化了不变的、流程化的内容,简化使用
b.回调允许我们在固化的流程中加入变化的内容
c.HibernateCallback接口
public Object execute(HibernateCallback action) throws DataAccessException
public List executeFind(HibernateCallback action) throws DataAccessException
范例:
1 public List<Employee> find(final int page, final int size) { 2 return getHibernateTemplate().executeFind( 3 new HibernateCallback() { 4 public Object doInHibernate(Session session) 5 throws HibernateException, SQLException { 6 Query query = session.createQuery("from Employee"); 7 query.setFirstResult((page - 1) * size); 8 query.setMaxResults(size); 9 return query.list(); 10 } 11 } 12 ); 13 }
Q:当把方法中的局部变量传递给它的内部类使用时,必须把该变量声明为final,但该方法已过时
A:推荐使用 findByCriteria(DetachedCriteria criteria)/findByCriteria(DetachedCriteria criteria, int firstResult, int maxResults)
3.Struts 2和Spring手动集成 -- 灵活配置Action(Spring配置文件中声明)
1).范例:
1 < !-- 在Spring配置文件中配置Action Bean,注意scope="prototype"属性 --> 2 <bean id="userAction" class="com.xuetang9.demo.action.UserAction" scope="prototype"> 3 < !-- 省略其他配置 --> 4 </bean> 5 6 < !-- 在Struts.xml中配置Action --> 7 <package name="login" extends="struts-default"> 8 <action name="login" class="userAction" method="login"> 9 < !-- 省略其他配置 --> 10 </action> 11 </package>
【注意:class属性的值不再是Action类的全限定名,而是Spring配置文件中相应的Action Bean的名称】
2).使用注意:
1. 采用此种配置方式时,Action的实例由Spring创建,Struts 2插件的工厂类只需根据Action Bean的id查找该组件使用即可
2. 此种方式可以为Action进行更灵活的配置,但代价是在Spring配置文件中需要定义很多Action Bean,增加了配置工作量,如非必需并非首选
3. 采用此种配置方式,同样需要添加struts2-spring-plugin-xxx.jar文件
4.Spring中Bean的作用域
作用域 说 明
singleton 默认值。Spring以单例模式创建Bean的实例,即容器中该Bean的实例只有一个
prototype 每次从容器中获取Bean时,都会创建一个新的实例
request 用于Web应用环境,针对每次HTTP请求都会创建一个实例
session 用于Web应用环境,同一个会话共享同一个实例,不同的会话使用不同的实例
global session 仅在Portlet的Web应用中使用,同一个全局会话共享一个实例。对于非Portlet环境,等同于session
注意:使用Web环境下的作用域,要在web.xml文件中配置 -- RequestContextListener或RequestContextFilter
范例:
1.实体类及其配置文件
a.User类
1 package com.Elastic.SpringDemo3.ivy.entity; 2 3 import java.io.Serializable; 4 5 public class User implements Serializable { 6 private String userName; 7 private String passWord; 8 9 public String getUserName() { 10 return userName; 11 } 12 public void setUserName(String userName) { 13 this.userName = userName; 14 } 15 public String getPassWord() { 16 return passWord; 17 } 18 public void setPassWord(String passWord) { 19 this.passWord = passWord; 20 } 21 }
b.User.hbm.xml
1 <?xml version="1.0"?> 2 <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" 3 "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> 4 <!-- Generated 2016-7-6 9:25:44 by Hibernate Tools 3.4.0.CR1 --> 5 <hibernate-mapping> 6 <class name="com.Elastic.SpringDemo3.ivy.entity.User" table="user"> 7 <id name="userName" type="java.lang.String"> 8 <column name="userName" /> 9 <generator class="assigned" /> 10 </id> 11 <property name="passWord" type="java.lang.String"> 12 <column name="passWord" /> 13 </property> 14 </class> 15 </hibernate-mapping>
2.dao包
a.UserDao接口
1 package com.Elastic.SpringDemo3.ivy.dao; 2 3 import java.io.Serializable; 4 import java.util.List; 5 6 import com.Elastic.SpringDemo3.ivy.entity.User; 7 8 public interface UserDao { 9 void save(User user); 10 11 void delete(User user); 12 13 void update(User user); 14 15 User findById(Serializable id); 16 17 List<User> find(); 18 }
3.dao.imp包
a.UserDaoImpl
1 package com.Elastic.SpringDemo3.ivy.dao.impl; 2 3 import java.io.Serializable; 4 import java.util.List; 5 6 import org.springframework.orm.hibernate3.support.HibernateDaoSupport; 7 8 import com.Elastic.SpringDemo3.ivy.dao.UserDao; 9 import com.Elastic.SpringDemo3.ivy.entity.User; 10 11 public class UserDaoImpl extends HibernateDaoSupport implements UserDao { 12 13 @Override 14 public void save(User user) { 15 this.getHibernateTemplate().save(user); 16 17 } 18 19 @Override 20 public void update(User user) { 21 this.getHibernateTemplate().update(user); 22 23 } 24 25 @Override 26 public User findById(Serializable id) { 27 return this.getHibernateTemplate().get(User.class, id); 28 29 } 30 31 @Override 32 public List<User> find() { 33 return this.getHibernateTemplate().find("from User "); 34 } 35 36 @Override 37 public void delete(User user) { 38 this.getHibernateTemplate().delete(user); 39 40 } 41 }
4.service包
a.UserService
1 package com.Elastic.SpringDemo3.ivy.service; 2 3 import com.Elastic.SpringDemo3.ivy.entity.User; 4 5 public interface UserService { 6 User login(String name, String pass); 7 }
5.service.impl包
a.UserServiceImpl
1 package com.Elastic.SpringDemo3.ivy.service.impl; 2 3 import com.Elastic.SpringDemo3.ivy.dao.UserDao; 4 import com.Elastic.SpringDemo3.ivy.entity.User; 5 import com.Elastic.SpringDemo3.ivy.service.UserService; 6 7 public class UserServiceImpl implements UserService { 8 9 private UserDao userDao; 10 11 public UserDao getUserDao() { 12 return userDao; 13 } 14 15 public void setUserDao(UserDao userDao) { 16 this.userDao = userDao; 17 } 18 19 @Override 20 public User login(String name, String pass) { 21 User user = userDao.findById(name); 22 if (null != user && user.getPassWord().equals(pass)) { 23 return user; 24 } 25 return null; 26 } 27 }
6.action包
a.UserAction
1 package com.Elastic.SpringDemo3.ivy.action; 2 3 import com.Elastic.SpringDemo3.ivy.entity.User; 4 import com.Elastic.SpringDemo3.ivy.service.UserService; 5 import com.opensymphony.xwork2.ActionContext; 6 import com.opensymphony.xwork2.ActionSupport; 7 8 public class UserAction extends ActionSupport { 9 private UserService userService; 10 private User user; 11 12 public User getUser() { 13 return user; 14 } 15 16 public void setUser(User user) { 17 this.user = user; 18 } 19 20 public UserService getUserService() { 21 return userService; 22 } 23 24 public void setUserService(UserService userService) { 25 this.userService = userService; 26 } 27 28 public String login() { 29 System.out.println(this); 30 User login = userService.login(user.getUserName(), user.getPassWord()); 31 32 if (null == login) { 33 return ERROR; 34 } 35 return SUCCESS; 36 } 37 }
7.Spring配置文件
a1. Struts 2和Spring集成 -- applicationContext.xml
1 <?xml version="1.0" encoding="UTF-8"?> 2 <beans xmlns="http://www.springframework.org/schema/beans" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xmlns:aop="http://www.springframework.org/schema/aop" 5 xmlns:tx="http://www.springframework.org/schema/tx" 6 xsi:schemaLocation="http://www.springframework.org/schema/beans 7 http://www.springframework.org/schema/beans/spring-beans-3.2.xsd 8 http://www.springframework.org/schema/aop 9 http://www.springframework.org/schema/aop/spring-aop-3.2.xsd 10 http://www.springframework.org/schema/tx 11 http://www.springframework.org/schema/tx/spring-tx-3.2.xsd"> 12 13 <!-- 使用Spring集成Hibernate(方法2:不需要hibernate配置文件) --> 14 <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"> 15 <property name="user" value="root"></property> 16 <property name="password" value="root"></property> 17 <property name="jdbcUrl" value="jdbc:mysql://localhost/hibernatedb"></property> 18 <property name="driverClass" value="com.mysql.jdbc.Driver"></property> 19 20 <!-- 连接池 --> 21 <property name="initialPoolSize" value="10"></property> 22 <property name="minPoolSize" value="5"></property> 23 <property name="maxPoolSize" value="100"></property> 24 <property name="acquireIncrement" value="3"></property> 25 <!-- 请求获取连接的重试次数 --> 26 <property name="acquireRetryAttempts" value="3"></property> 27 <property name="acquireRetryDelay" value="5"></property> 28 <property name="idleConnectionTestPeriod" value="60"></property> 29 </bean> 30 31 <!-- 使用dataSource配置SessionFactory --> 32 <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"> 33 <property name="dataSource" ref="dataSource"></property> 34 <property name="hibernateProperties"> 35 <props> 36 <prop key="dialect">org.hibernate.dialect.MySQLDialect</prop> 37 <prop key="show_sql">true</prop> 38 <prop key="format_sql">true</prop> 39 </props> 40 </property> 41 <!-- 通配 --> 42 <property name="mappingLocations" value="classpath:com/Elastic/SpringDemo3/ivy/entity/*.hbm.xml"><!-- 注意classpath的拼写 --> 43 </property> 44 </bean> 45 46 <!-- 配置事务 --> 47 <!-- 配置事务管理器(增强的功能/切面) --> 48 <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager"> 49 <property name="sessionFactory" ref="sessionFactory"></property> 50 </bean> 51 <!-- 事务级别(真正的增强功能) --> 52 <tx:advice id="tx" transaction-manager="transactionManager"> 53 <tx:attributes> 54 <!-- 方法细分 --> 55 <!-- [propagation(事务级别)]REQUIRED(默认): 如果前面已经开启事务,把这个事务合并到上一个事务中 ,并把当前方法放进合并后的事务中 --> 56 <!-- read-only(只读):查询。防止脏数据。 --> 57 <tx:method name="login" propagation="REQUIRED" read-only="true" timeout="3"/> 58 <!-- 通配 --> 59 <tx:method name="search*" propagation="REQUIRED" read-only="true" timeout="10"/> 60 <tx:method name="modify*" propagation="REQUIRED" rollback-for="java.lang.Exception"/> 61 </tx:attributes> 62 </tx:advice> 63 <!-- 配置哪些方法需要使用事务 --> 64 <aop:config> 65 <aop:pointcut expression="execution(* com.Elastic.SpringDemo3.ivy.service..*.*(..))" id="txpointcut"/> 66 <aop:advisor advice-ref="tx" pointcut-ref="txpointcut"/> 67 </aop:config> 68 69 <!-- 配置Dao --> 70 <bean id="userDao" class="com.Elastic.SpringDemo3.ivy.dao.impl.UserDaoImpl"> 71 <property name="sessionFactory" ref="sessionFactory"></property> 72 </bean> 73 74 <!-- 配置Service --> 75 <bean id="userService" class="com.Elastic.SpringDemo3.ivy.service.impl.UserServiceImpl"> 76 <property name="userDao" ref="userDao"></property> 77 </bean> 78 79 </beans>
a2. Struts 2和Spring手动集成:Action声明在Spring的配置文件 -- applicationContext.xml
注意:这里的Action是单例模式,Action声明中必须要有scope属性
1 <?xml version="1.0" encoding="UTF-8"?> 2 <beans xmlns="http://www.springframework.org/schema/beans" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xmlns:aop="http://www.springframework.org/schema/aop" 5 xmlns:tx="http://www.springframework.org/schema/tx" 6 xsi:schemaLocation="http://www.springframework.org/schema/beans 7 http://www.springframework.org/schema/beans/spring-beans-3.2.xsd 8 http://www.springframework.org/schema/aop 9 http://www.springframework.org/schema/aop/spring-aop-3.2.xsd 10 http://www.springframework.org/schema/tx 11 http://www.springframework.org/schema/tx/spring-tx-3.2.xsd"> 12 13 <!-- 使用Spring集成Hibernate(方法2:不需要hibernate配置文件) --> 14 <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"> 15 <property name="user" value="root"></property> 16 <property name="password" value="root"></property> 17 <property name="jdbcUrl" value="jdbc:mysql://localhost/hibernatedb"></property> 18 <property name="driverClass" value="com.mysql.jdbc.Driver"></property> 19 20 <!-- 连接池 --> 21 <property name="initialPoolSize" value="10"></property> 22 <property name="minPoolSize" value="5"></property> 23 <property name="maxPoolSize" value="100"></property> 24 <property name="acquireIncrement" value="3"></property> 25 <!-- 请求获取连接的重试次数 --> 26 <property name="acquireRetryAttempts" value="3"></property> 27 <property name="acquireRetryDelay" value="5"></property> 28 <property name="idleConnectionTestPeriod" value="60"></property> 29 </bean> 30 31 <!-- 使用dataSource配置SessionFactory --> 32 <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"> 33 <property name="dataSource" ref="dataSource"></property> 34 <property name="hibernateProperties"> 35 <props> 36 <prop key="dialect">org.hibernate.dialect.MySQLDialect</prop> 37 <prop key="show_sql">true</prop> 38 <prop key="format_sql">true</prop> 39 </props> 40 </property> 41 <!-- 通配 --> 42 <property name="mappingLocations" value="classpath:com/Elastic/SpringDemo3/ivy/entity/*.hbm.xml"><!-- 注意classpath的拼写 --> 43 </property> 44 </bean> 45 46 <!-- 配置事务 --> 47 <!-- 配置事务管理器(增强的功能/切面) --> 48 <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager"> 49 <property name="sessionFactory" ref="sessionFactory"></property> 50 </bean> 51 <!-- 事务级别(真正的增强功能) --> 52 <tx:advice id="tx" transaction-manager="transactionManager"> 53 <tx:attributes> 54 <!-- 方法细分 --> 55 <!-- [propagation(事务级别)]REQUIRED(默认): 如果前面已经开启事务,把这个事务合并到上一个事务中 ,并把当前方法放进合并后的事务中 --> 56 <!-- read-only(只读):查询。防止脏数据。 --> 57 <tx:method name="login" propagation="REQUIRED" read-only="true" timeout="3"/> 58 <!-- 通配 --> 59 <tx:method name="search*" propagation="REQUIRED" read-only="true" timeout="10"/> 60 <tx:method name="modify*" propagation="REQUIRED" rollback-for="java.lang.Exception"/> 61 </tx:attributes> 62 </tx:advice> 63 <!-- 配置哪些方法需要使用事务 --> 64 <aop:config> 65 <aop:pointcut expression="execution(* com.Elastic.SpringDemo3.ivy.service..*.*(..))" id="txpointcut"/> 66 <aop:advisor advice-ref="tx" pointcut-ref="txpointcut"/> 67 </aop:config> 68 69 <!-- 配置Dao --> 70 <bean id="userDao" class="com.Elastic.SpringDemo3.ivy.dao.impl.UserDaoImpl"> 71 <property name="sessionFactory" ref="sessionFactory"></property> 72 </bean> 73 74 <!-- 配置Service --> 75 <bean id="userService" class="com.Elastic.SpringDemo3.ivy.service.impl.UserServiceImpl"> 76 <property name="userDao" ref="userDao"></property> 77 </bean> 78 79 <!-- 配置Action --> 80 <bean id="userAction" class="com.Elastic.SpringDemo3.ivy.action.UserAction" scope="prototype"> 81 <property name="userService" ref="userService"></property> 82 </bean> 83 </beans>
8.Struts配置文件
a1. Struts 2和Spring集成 -- struts.xml
1 <?xml version="1.0" encoding="UTF-8"?> 2 <!DOCTYPE struts PUBLIC 3 "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN" 4 "http://struts.apache.org/dtds/struts-2.3.dtd"> 5 6 <struts> 7 <package name="default" namespace="/" extends="struts-default"> 8 <action name="login" class="com.Elastic.SpringDemo3.ivy.action.UserAction" method="login"> 9 <result name="success">/index.jsp</result> 10 <result name="error">/error.jsp</result> 11 </action> 12 </package> 13 14 </struts>
a2. Struts 2和Spring手动集成 -- struts.xml
1 <?xml version="1.0" encoding="UTF-8"?> 2 <!DOCTYPE struts PUBLIC 3 "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN" 4 "http://struts.apache.org/dtds/struts-2.3.dtd"> 5 6 <struts> 7 <package name="default" namespace="/" extends="struts-default"> 8 <!-- class属性不再是全限定名,而是Spring配置文件中的Action声明的id --> 9 <action name="login" class="userAction" method="login"> 10 <result name="success">/index.jsp</result> 11 <result name="error">/error.jsp</result> 12 </action> 13 </package> 14 15 </struts>
9.jsp
a.login.jsp
1 <%-- 引入JSP页面PAGE指令 --%> 2 <%@ page language="java" contentType="text/html; charset=UTF-8" 3 pageEncoding="UTF-8"%> 4 <%-- 引入JSTL标签指令 --%> 5 <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%> 6 <%@ taglib uri="/struts-tags" prefix="s"%> 7 <!DOCTYPE html> 8 <html language="zh-CN"> 9 <head> 10 <meta charset="utf-8"> 11 <!-- 设置浏览器渲染的引擎 --> 12 <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1"> 13 <!-- 设置支持移动设备 --> 14 <meta name="viewport" content="width=device-width, initial-scale=1"> 15 <title>网页标题</title> 16 <!-- 引用bootstrap样式 --> 17 <link rel="stylesheet" type="text/css" 18 href="<%=request.getContextPath()%>/css/bootstrap.min.css"> 19 </head> 20 <body> 21 <s:debug></s:debug> 22 <div class="container-fluid"> 23 <div class="panel panel-default center-block" style="width: 500px;"> 24 <div class="panel-heading">登录</div> 25 <div class="panel-body"> 26 <form action="login" method="post" class="form-horizontal"> 27 <div class="form-group"> 28 <label class="col-md-3 control-label" for="userName">用户名:</label> 29 <div class="col-md-6"> 30 <input class="form-control" id="userName" name="user.userName" 31 type="text" autocomplete="off" /> 32 </div> 33 <div class="col-md-3"></div> 34 </div> 35 <div class="form-group"> 36 <label class="col-md-3 control-label" for="passWord">密码:</label> 37 <div class="col-md-6"> 38 <input class="form-control" id="passWord" name="user.passWord" 39 type="password" /> 40 </div> 41 <div class="col-md-3"></div> 42 </div> 43 <div class="form-group"> 44 <div class="col-md-offset-3 col-md-6"> 45 <input class="btn btn-primary btn-block" type="submit" value="登录" /> 46 </div> 47 </div> 48 </form> 49 </div> 50 </div> 51 </div> 52 53 <!-- 引用外部JS文件 --> 54 <script type="text/javascript" 55 src="<%=request.getContextPath()%>/js/jquery-2.2.4.js"></script> 56 <script type="text/javascript" 57 src="<%=request.getContextPath()%>/js/bootstrap.min.js"></script> 58 59 </body> 60 </html>
10.web.xml
1 <?xml version="1.0" encoding="UTF-8"?> 2 <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5"> 3 <display-name>SpringDemo3_ivy</display-name> 4 <welcome-file-list> 5 <welcome-file>index.html</welcome-file> 6 <welcome-file>index.htm</welcome-file> 7 <welcome-file>index.jsp</welcome-file> 8 <welcome-file>default.html</welcome-file> 9 <welcome-file>default.htm</welcome-file> 10 <welcome-file>default.jsp</welcome-file> 11 </welcome-file-list> 12 13 <!-- 配置Spring文件的位置 --> 14 <context-param> 15 <param-name>contextConfigLocation</param-name> 16 <param-value>classpath:applicationContext.xml</param-value> 17 </context-param> 18 19 <!-- 配置的是Spring的启动监听器 --> 20 <listener> 21 <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> 22 </listener> 23 24 <!-- 配置OpenSessionInView模式 --> 25 <filter> 26 <filter-name>OpenSessionInView</filter-name> 27 <filter-class>org.springframework.orm.hibernate3.support.OpenSessionInViewFilter</filter-class> 28 </filter> 29 <filter-mapping> 30 <filter-name>OpenSessionInView</filter-name> 31 <url-pattern>/*</url-pattern> 32 </filter-mapping> 33 34 <!-- 配置Struts2 核心过滤器 --> 35 <filter> 36 <filter-name>Struts2</filter-name> 37 <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class> 38 </filter> 39 <filter-mapping> 40 <filter-name>Struts2</filter-name> 41 <url-pattern>/*</url-pattern> 42 </filter-mapping> 43 44 <!-- 配置HttpSession过期时间(单位:分钟) --> 45 <session-config> 46 <session-timeout>20</session-timeout> 47 </session-config> 48 </web-app>