Struts2和Spring的整合

一、首先導入Struts2和Spring的必備架包


二、導入Struts2和Spring的主配置文件

三、導入Spring和Struts2整合的架包

       

四、在web.xml中配置ApplicationContext容器的監聽器

      

<span style="font-size:18px;"><?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">
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
  
  <!-- 配置Spring用於初始化的ApplicationContext的監聽器 -->
  <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>
  <context-param>
     <param-name>contextConfigLocation</param-name>
     <param-value>/WEB-INF/classes/applicationContext*.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>
</web-app>
</span>
五、兩個主配置文件

       1. struts.xml

<span style="font-size:18px;"><?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
    "http://struts.apache.org/dtds/struts-2.0.dtd">

<struts>
    <!-- 開發模式 -->
    <constant name="struts.devMode" value="true" />    
    
    <!--  -->
    <package name="test" extends="struts-default" namespace="/">
       <action name="test" class="testAction"><!-- 這裏的 testAction是bean的名稱,完成了與Spring的整合-->
           <result name="success">/test.jsp</result>
       </action>
    </package>
</struts>
</span>
 

    2.applicationContext.xml  (這裏的Spring的使用的是註解注入的方式)

<span style="font-size:18px;"><?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"
		xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.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">
    
    <!-- 註解注入,配置掃描哪個包下 -->
    <context:component-scan base-package="com.seven"></context:component-scan>
   
</beans>
</span>

六、UserService

<span style="font-size:18px;">package com.seven.service;

import org.springframework.stereotype.Service;

@Service("userService")
public class UserService {
	//模擬保存用戶
	public void saveUser(Object user){
		System.out.println("保存了一個用戶");
	}

}
</span>
七、配置Struts的Action(多例)

<span style="font-size:18px;">package com.seven.action;

import javax.annotation.Resource;
import javax.servlet.ServletContext;

import org.apache.struts2.ServletActionContext;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.context.support.WebApplicationContextUtils;

import com.opensymphony.xwork2.ActionSupport;
import com.seven.service.UserService;
@Controller("testAction")
@Scope("prototype")//配置多例
public class TestAction extends ActionSupport{
	private String id;
	
	@Resource(name="userService")
	private UserService userService;

	public String execute() throws Exception {
		System.out.println("TestAction.execute()");
	    //獲取application
        ServletContext application = ServletActionContext.getServletContext();
        //使用工具類獲取ApplicationContext對象,這裏的ApplicationContext對象全局只有唯一一個,即在web.xml監聽器中初始化的那個
        ApplicationContext ac = WebApplicationContextUtils.getWebApplicationContext(application);
        //獲取bean中的userService
	    userService = (UserService) ac.getBean("userService");
	    userService.saveUser(new Object());
		return SUCCESS;
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}
	
   
}
</span>








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