Spring 整合Structs

1  在web.xml 中做Spring的和Structs2 的基本配置

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" 
	xmlns="http://java.sun.com/xml/ns/javaee" 
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
	http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
  <display-name></display-name>	
  <!--   配置Structs2 的核心过滤器 -->
  <filter>
      <filter-name>structs2</filter-name>
      <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter      </filter-class>
  </filter>
  <filter-mapping>
         <filter-name>structs2</filter-name>
         <url-pattern>/*</url-pattern>
  </filter-mapping>
  
  <!-- 配置Spring的配置文件 -->
  <context-param>
      <param-name>contextConfigLocation</param-name>
      <param-value>
          classpath:beans.xml  <!-- Spring 配置文件所在的位置classPath表示Src 下的路径 -->
             <!--  可以配置多个   /WEB-INF/conf/conf.xml  用“,”隔开  表示在/WEB-INF/conf                 /conf.xml的路径下 --> 
      </param-value>
  </context-param>
  <listener>   
            <!-- 用Spring的contextLoaderListener 在项目启动时就加载Spring的配置文件  -->
         <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
</web-app>

2  创建action  

public class StudAction  extends ActionSupport{
	    private IStudService service;
	    private String name; 
        public String getName() {
			return name;
		}
		public void setName(String name) {
			this.name = name;
		}
		public IStudService getService() {
			return service;
		}
		public void setService(IStudService service) {
			this.service = service;
		}
		public String execute() throws Exception{
        	         System.err.println("这个是AtudAction"+getName());
        	         service.save();
        	         return SUCCESS;
           }
}

3 配置创建Structs2的类工厂

package com.wuyihuai.factory;

import java.util.Map;

import javax.servlet.ServletContext;

import org.apache.struts2.ServletActionContext;
import org.springframework.context.ApplicationContext;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ObjectFactory;

/**
 * 让Structs2 不去创建action 而是去Spring中取需要的Action
 * */
public class MyObjectFactory extends ObjectFactory{
	@Override
	public Object buildBean(Class cls, Map<String, Object> ctx)
			throws Exception {
		// TODO Auto-generated method stub
		System.err.println("创建了。。。。。。"+cls+""+ctx);
		
		//判断cls 是不是Action 的孩子 如果不断地的话由于struct的过滤器在Spring的bean.xml中找不到会报错
		if(Action.class.isAssignableFrom(cls)){
			 //是Action Struct2不创建,由Spring的 创建
			ServletContext cs=ServletActionContext.getServletContext();
			//此类是以经封装好的beans.xml中的对象数据
		//	ApplicationContext appCtx=(ApplicationContext) cs.getAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE);  
			ApplicationContext appCtx=WebApplicationContextUtils.getWebApplicationContext(cs);
			Object o=appCtx.getBean(cls);
			return o;
		}
		else{
			  //不是Action 就有Struct2自己创建
			  return super.buildBean(cls, ctx);
		}
		
	}
       
}
4 配置Struct2.xml文件

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC 
"-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" 
"http://struts.apache.org/dtds/struts-2.1.dtd">
<struts>
                <!-- 配置自己的类工厂 -->
               <bean type="com.opensymphony.xwork2.ObjectFactory" name="wuyihuai"
                  class="com.wuyihuai.factory.MyObjectFactory" /> 
                 <constant name="struts.objectFactory" value="wuyihuai" ></constant>
                 <!-- 配置Action类 -->
                 <package name="wuyihuai" extends="struts-default">
                 <action name="stud" class="com.wuyihuai.stud.StudAction">
                            <result>/index.jsp</result> 
                   </action>
               </package>
</struts>

5 配置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:aop="http://www.springframework.org/schema/aop"
		xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
		        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
				http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
				http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
       
       <!-- 配置action bean -->
       <bean id="studAction" class="com.wuyihuai.stud.StudAction" scope="prototype">
            <property name="service" ref="studService"></property>
       </bean>
        <!-- 声明自己的dao和service -->
       <bean id="studService" class="com.wuyihuai.service.StudServiceImpl">
                 <property name="dao">
                     <bean class="com.wuyihuai.dao.StudDaoJdbc"></bean>
                 </property>
       </bean>
</beans>



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值