欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

struts和spring的整合方式对比 博客分类: Java编程springstruts strutsspringjavawebxml 

程序员文章站 2024-03-23 09:10:52
...
1.业务Action类继承ActionSupport类

 

业务Action中可以调用ActionSupport类中的getWebApplicationContext(),来获得wac,然后

调用wac.getBean("myservice")来获得需要的service对象

 

这种方法虽然可以,但是不推荐使用,原因如下:

1>业务Action类跟Spring的ActionSupport类藕合在一起不是很好

2>getWebApplicationContext().getBean("myservice")这种硬编码不是很好

3>这种方法Spring并没有托管Action对象

 

2.使用Spring的DelegatingRequestProcessor类来替代struts的RequestProcessor类

 

struts-config.xml

<controller 
	locale= "true"
	processorClass= "org.springframework.web.struts.DelegatingRequestProcessor">
</controller>

 

DelegatingRequestProcessor类继承了RequestProcessor类,并重写了RequestProcessor的一些方法

 

DelegatingRequestProcessor.java

public class DelegatingRequestProcessor extends RequestProcessor {

	private WebApplicationContext webApplicationContext;
	

	public void init(ActionServlet actionServlet, ModuleConfig moduleConfig) throws ServletException {
		super.init(actionServlet, moduleConfig);
		if (actionServlet != null) {
			this.webApplicationContext = initWebApplicationContext(actionServlet, moduleConfig);
		}
	}

	protected WebApplicationContext initWebApplicationContext(
			ActionServlet actionServlet, ModuleConfig moduleConfig) throws IllegalStateException {

		return DelegatingActionUtils.findRequiredWebApplicationContext(actionServlet, moduleConfig);
	}

	protected final WebApplicationContext getWebApplicationContext() {
		return this.webApplicationContext;
	}

	protected Action processActionCreate(
			HttpServletRequest request, HttpServletResponse response, ActionMapping mapping)
			throws IOException {

		Action action = getDelegateAction(mapping);
		if (action != null) {
			return action;
		}
		return super.processActionCreate(request, response, mapping);
	}

	protected Action getDelegateAction(ActionMapping mapping) throws BeansException {
		String beanName = determineActionBeanName(mapping);
		if (!getWebApplicationContext().containsBean(beanName)) {
			return null;
		}
		return (Action) getWebApplicationContext().getBean(beanName, Action.class);
	}

	protected String determineActionBeanName(ActionMapping mapping) {
		return DelegatingActionUtils.determineActionBeanName(mapping);
	}

}

 

1>调用init方法的时候,初始化并获得wac

2>调用processActionCreate方法的时候,先从wac中getBean("prefix+path"),如果找不到再从struts内部获取

 

3.所有的业务Action都配置成DelegatingActionProxy类

 

struts-config.xml

<action-mappings>
    	<action path="/login" type="org.springframework.web.struts.DelegatingActionProxy" name="userForm" scope="request">
            <forward name="success" path="/WEB-INF/jsp/index.jsp"/>
        </action>
</action-mappings>

 

DelegatingActionProxy.java

public class DelegatingActionProxy extends Action {


	public ActionForward execute(
			ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)
			throws Exception {

		Action delegateAction = getDelegateAction(mapping);
		return delegateAction.execute(mapping, form, request, response);
	}


	
	protected Action getDelegateAction(ActionMapping mapping) throws BeansException {
		WebApplicationContext wac = getWebApplicationContext(getServlet(), mapping.getModuleConfig());
		String beanName = determineActionBeanName(mapping);
		return (Action) wac.getBean(beanName, Action.class);
	}

	
	protected WebApplicationContext getWebApplicationContext(
			ActionServlet actionServlet, ModuleConfig moduleConfig) throws IllegalStateException {

		return DelegatingActionUtils.findRequiredWebApplicationContext(actionServlet, moduleConfig);
	}

	
	protected String determineActionBeanName(ActionMapping mapping) {
		return DelegatingActionUtils.determineActionBeanName(mapping);
	}

}

 

DelegatingActionProxy重写了execute方法,当struts调用execute方法的时候,内部会调用wac.getBean("prefix+path")获取被Spring托管的Action对象,然后再调用业务Action对象的execute方法,其实就是利用了代理模式对Action进行代理

 

Spring的配置文件:

 

applicationContext.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"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
           http://www.springframework.org/schema/beans/spring-beans.xsd">
  	
	<bean id="loginService" class="prd.tidy.test.service.LoginService">
			
	</bean>
	
	<bean name="/login" class="prd.tidy.test.action.LoginAction" scope="prototype">
		<property name="loginService" ref="loginService">
		
		</property>
	</bean>
    
</beans>