1. 概述

spring和struts整合:
	1.创建web程序
	2.引入struts2类库.
	3.创建HelloWorldAction
		package cn.itcast.struts2.action;
		import com.opensymphony.xwork2.ActionSupport;
		/**
		 * HelloWorldAction
		 */
		public class HelloWorldAction extends ActionSupport {
			private static final long serialVersionUID = 6480501738385774728L;
			public String reg() {
				System.out.println("hello world");
				return SUCCESS;
			}
		}
	3.配置web.xml
		<?xml version="1.0" encoding="UTF-8"?>
		<web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
			xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee 
			http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
			<filter>
				<filter-name>action</filter-name>
				<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
			</filter>
			<filter-mapping>
				<filter-name>action</filter-name>
				<url-pattern>/*</url-pattern>
			</filter-mapping>
			<welcome-file-list>
				<welcome-file>index.jsp</welcome-file>
			</welcome-file-list>
		</web-app>
	4.配置struts.xml
		<?xml version="1.0"?>
		<!DOCTYPE struts PUBLIC
			"-//Apache Software Foundation//DTD Struts Configuration 2.1.7//EN"
			"http://struts.apache.org/dtds/struts-2.1.7.dtd">
		<struts>
			<constant name="struts.devMode" value="true" />
			<package name="helloworldPkg" extends="struts-default">
				<action name="HelloWorldAction_*" class="cn.itcast.struts2.action.HelloWorldAction" method="{1}">
					<result name="success">/index.jsp</result>
				</action>
			</package>
		</struts>

	5.引入spring类库 + struts2-spring-plugin-2.1.8.1.jar
	6.配置web.xml
		<!-- 通过上下文参数指定spring配置文件的位置 -->
		<context-param>
			<param-name>contextConfigLocation</param-name>
			<param-value>classpath:beans.xml</param-value>
		</context-param>
		
		<!-- 上下文载入器监听器,确保web服务器启动时,直接完成spring容器的初始化,将ac放到application范围中 -->
		<listener>
			<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
		</listener>

	7.修改struts.xml配置文件中的action
		把action的class改成bean的id.
		<!-- class:指定action在spring容器的id -->
		<action name="HelloWorldAction_*" class="helloWorldAction" method="{1}">
		

struts2加载顺寻:
	1.struts-core.jar/struts.xml
	2.xxx-plugin.xml / struts-plugin.xml
	3.project/struts.xml

ac = new Classpathxmlac(new String[]{a.xml,b.xml});


2. 示例代码

HelloWorldService.java, service接口

public interface HelloWorldService {
	public void sayHello();
}

HelloWorldServiceImpl.java, service实现

//方式二:注解配置
@Service("helloWorldService")
public class HelloWorldServiceImpl implements HelloWorldService {
	public void sayHello() {
		System.out.println("this is a service");
	}
}

HelloWorldAction.java, 处理请求的action

/**
 * HelloWorldAction, 方式二:注解配置
 */
@Controller("helloWorldAction")
@Scope("prototype")
public class HelloWorldAction extends ActionSupport {
	private static final long serialVersionUID = 6480501738385774728L;
	
	@Resource()
	private HelloWorldService hws ;
	
	public HelloWorldAction(){
		System.out.println("new HelloWorldAction()");
	}
	public String reg() {
		ServletActionContext.getServletContext().getAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE);
		hws.sayHello();
		System.out.println("hello world");
		return SUCCESS;
	}
}

beans.xml,spring配置bean, 只配置bean

actions.xml,spring配置action

struts.xml, struts配置

web.xml







 

相关文章: