1:struts与Spring的整合
1:加载2个框架的jar包。
2:加载Struts与Spring结合的jar包。struts2-spring-plugin-2.3.28.jar
把Struts中Action类交给Spring框架来控制。就可以使用DI的功能往Action中注入所需要用到Service对象。
3:进行相应的配置。
web.xml
//web中实例化Spring的ApplicationContext上下文环境。
//SE ApplicationContext context = new ClassPathXmlAppliationContext("spring.xml");
//WEB:配一个Spring的监听器。
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring.xml</param-value>
</context-param>
ContextLoaderListener默认读取/WEB-INF/applicationContext.xml这个文件。
可以通过contextConfigLocation来进行更改读取的配置文件的路径。
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
//编码过滤器
<filter>
<filter-name>EncodingFilter</filter-name>
<filter-class>com.filter.EncodingFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>EncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
//struts过滤器
<filter>
<filter-name>struts2</filter-name>
<filter-class>com.filter.StrutsFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
struts.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>
<constant name="struts.i18n.encoding" value="UTF-8"></constant>
<constant name="struts.multipart.maxSize" value="209715200"></constant>
<constant name="struts.action.extension" value="action,,"></constant>
<constant name="struts.enable.DynamicMethodInvocation" value="true" />
<constant name="struts.devMode" value="true" />
<constant name="struts.configuration.xml.reload" value="true"></constant>
<constant name="struts.i18n.reload" value="true"></constant>
<constant name="struts.ui.theme" value="simple"></constant>
<constant name="struts.ognl.allowStaticMethodAccess" value="true"></constant>
<!-- struts对象交给Spring管理的2个配置 -->
<constant name="struts.objectFactory" value="spring"></constant>
<constant name="struts.objectFactory.spring.autoWire"
value="name">
</constant>
<package name="default" namespace="/" extends="struts-default">
</package>
</struts>
spring.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:jdbc="http://www.springframework.org/schema/jdbc"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.1.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.1.xsd
http://www.springframework.org/schema/jdbc
http://www.springframework.org/schema/jdbc/spring-jdbc-3.1.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd">
<context:annotation-config></context:annotation-config>
<context:component-scan base-package="com.serivce"></context:component-scan>
<context:component-scan base-package="com.dao"></context:component-scan>
<bean name="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName"
value="com.microsoft.sqlserver.jdbc.SQLServerDriver">
</property>
<property name="url"
value="jdbc:sqlserver://127.0.0.1:1433;databaseName=java1603">
</property>
<property name="username" value="sa"></property>
<property name="password" value=""></property>
</bean>
<bean name="transactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"></property>
</bean>
<tx:annotation-driven transaction-manager="transactionManager" />
</beans>
4:写Action,Servcie,Dao
3.17(2)struts和spring框架的整合
最新推荐文章于 2018-09-29 15:03:22 发布