集成Struts
1,拷贝jar和xml(struts2.xml,struts2的jar包,web.xml)
1)相关的jar以及spring-web.*.jar,struts2-spring-plugin.*.jar
2)struts.xml
3)web.xml
①:Struts相关的包:
②:Spring中需要的jar包:
③:Spring和Struts连接的包:
2)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> <include file="struts-default.xml"></include> <package name="hello" extends="struts-default">
编写struts的配置文件:
注意,在action的class属性,填写的是这个Action在applicationContext.xml中对应的bean的id; <action name="stu_*" class="Staction" method="{1}"> <result name="list">/WEB-INF/views/Student.jsp</result> </action> </package> </struts>
3)web.xml
<?xml version="1.0" encoding="UTF-8"?> <web-app id="WebApp_9" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"> 在web.xml中添加listener <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener>
在web.xml中添加spring框架启动的加载的配置文件路径:
<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.ng.filter.StrutsPrepareAndExecuteFilter</filter-class> </filter> <filter-mapping> <filter-name>struts2</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> </web-app>
2,编写Action;
package com.gxxy.action; import java.util.List; import javax.servlet.http.HttpServletRequest; import org.apache.struts2.ServletActionContext; import com.gxxy.domain.Student; import com.gxxy.service.IStudentServiceDAO; import com.opensymphony.xwork2.ActionSupport; public class StudentAction extends ActionSupport { private static final long serialVersionUID = 1L; private IStudentServiceDAO service; public void setService(IStudentServiceDAO service) { this.service = service; } public String list() throws Exception { List<Student> list = service.list(); HttpServletRequest request = ServletActionContext.getRequest(); request.setAttribute("stu", list); return "list"; } }
3,在applicationContext.xml中配置Action;
* 注意, 因为Action不是线程安全的, 所以需要每次请求创建一个新的Action, 所以在配置这个bean的时候
scope需要填写prototype
<!-- 配置Action --> <bean id="Staction" class="com.gxxy.action.StudentAction" scope="prototype"> <property name="service" ref="service"></property> </bean>
4,编写struts的配置文件:
* 注意,在action的class属性,填写的是这个Action在applicationContext.xml中对应的bean的id;
5,在web.xml中添加listener
6,在web.xml中添加spring框架启动的加载的配置文件路径:
<context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:applicationContext.xml</param-value> </context-param>
ApplicationContext.xml的所有配置:包括Hibernate配置:
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd "> <!-- 加载配置文件内容 jdbc.driverClassName=com.mysql.jdbc.Driver jdbc.url=jdbc:mysql:///test jdbc.username=root jdbc.password=root --> <context:property-placeholder location="classpath:db_propeties"/> <!-- 配置连接数据库的连接池 dataSource--> <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" destroy-method="close"> <property name="driverClassName" value="${jdbc.driverClassName}"/> <property name="url" value="${jdbc.url}"/> <property name="username" value="${jdbc.username}"/> <property name="password" value="${jdbc.password}"/> </bean> <!-- 配置一个SessionFactory --> <bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"> <!-- 配置连接数据库的信息 --> <property name="dataSource" ref="dataSource"></property> <!-- 配置hibernate的常规相关配置 --> <property name="hibernateProperties"> <value> hibernate.dialect=org.hibernate.dialect.MySQLDialect hibernate.hbm2ddl.auto=none hibernate.show_sql=true </value> </property> <!-- 配置*.hbm.xml的位置(告诉Spring我的*.hbm.xml在哪个包中) 注意:value值为路径 --> <property name="mappingDirectoryLocations" value="classpath:com/gxxy/domain"></property> <!-- 配置多个映射文件的写法 <property name="mappingDirectoryLocations"> <list> <value>com/gxxy/ssh/proj/domain</value> <value>com/gxxy/ssh/proj/domain</value> <value>com/gxxy/ssh/proj/domain</value> <value>com/gxxy/ssh/proj/domain</value> <value>com/gxxy/ssh/proj/domain</value> <value>com/gxxy/ssh/proj/domain</value> <value>com/gxxy/ssh/proj/domain</value> </list> </property> --> </bean> <!-- Dao --> <bean id="dao" class="com.gxxy.dao.impl.StudentDaoImpl"> <property name="sessionFactory" ref="sessionFactory"></property> </bean> <!-- service --> <bean id="service" class="com.gxxy.service.impl.StudnetServiceImpl"> <property name="dao" ref="dao"></property> </bean> <!-- 配置事务管理器 --> <bean id="txMgr" class="org.springframework.orm.hibernate4.HibernateTransactionManager"> <property name="sessionFactory" ref="sessionFactory"></property> </bean> <!-- 配置事务属性 --> <aop:config> <aop:pointcut expression="execution(* com.gxxy.service.*.*(..))" id="stuService"/> <aop:advisor advice-ref="advice1" pointcut-ref="stuService"/> </aop:config> <tx:advice id="advice1" transaction-manager="txMgr"> <tx:attributes> <tx:method name="get*" read-only="true"/> <tx:method name="list*" read-only="true"/> <tx:method name="*"/> </tx:attributes> </tx:advice> <!-- 配置Action --> <bean id="Staction" class="com.gxxy.action.StudentAction" scope="prototype"> <property name="service" ref="service"></property> </bean> </beans>