3.17(2)struts和spring框架的整合

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
		
	

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值