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>



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章