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>








评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值