SSH框架的整合之一:struts2和Spring框架的整合

Struts2和Spring的整合:将struts2的action对象创建交给了spring惊醒管理

        未整合的Struts2的创建

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>
	<package name="demo1" extends="struts-default" namespace="/">
		<!-- class属性里面不写action的全路径了,因为写了,action对象就创建了两次(一次struts,一次spring)
			所以class里面直接写spring配置中的此action相对应的bean的id值
		 -->
		<!--这是原来的未整合的写法 
		<action name="userAction" class="cn.itcast.action.UserAction"></action> -->
		<!-- 正确的写法是 -->
		<action name="userAction" class="userAction"></action>
	</package>
	
</struts>

spring的配置文件如下:创建了一个id为userAction的bean

<?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: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.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">
    	
    <!-- 配置c3p0连接池 --> 
    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
    	<!-- 注入属性值 -->
    	<property name="driverClass" value="com.mysql.jdbc.Driver"></property>
    	<property name="jdbcUrl" value="jdbc:mysql///spring_day04"></property>
    	<property name="user" value="root"></property>
    	<property name="password" value="root"></property>	
    </bean>    	
    
    <bean id="userAction" class="cn.itcast.action.UserAction" scope="prototype"></bean>

</beans>

本来action标签中的class指向的是一个有完整路径的类,而整合之后,class的值指向了spring配置文件中的bean的id。

web.xml配置如下:

<?xml version="1.0" encoding="UTF-8"?>
<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">
  <display-name>spring_day04_sshdemo1</display-name>
  
  <context-param>
  	<param-name>contextConfigLocation</param-name>
  	<param-value>classpath:bean1.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>
  
  <listener>
  	<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>
  
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>
</web-app>

同样的配置struts2的过滤器 和spring的监听器,更改spring加载配置文件xml的classpath路径。

action演示类如下

package cn.itcast.action;

import com.opensymphony.xwork2.ActionSupport;

public class UserAction extends ActionSupport {

	@Override
	public String execute() throws Exception {
		System.out.println("action.............");
		return NONE;
	}

	
}





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值