Comparison of the integration methods of struts and spring

1. The business Action class inherits the ActionSupport class

 

In the business Action, you can call getWebApplicationContext() in the ActionSupport class to get the wac, and then

Call wac.getBean("myservice") to get the required service object

 

Although this method is possible, it is not recommended for the following reasons:

1> The business Action class is not very well coupled with Spring's ActionSupport class

2>getWebApplicationContext().getBean("myservice") This hard coding is not very good

3> This method Spring does not manage the Action object

 

2. Use Spring's DelegatingRequestProcessor class to replace struts' RequestProcessor class

 

struts-config.xml

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

 

The DelegatingRequestProcessor class inherits the RequestProcessor class and rewrites some methods of 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> When calling the init method, initialize and get wac

2> When calling the processActionCreate method, first getBean("prefix+path") from wac, if not, then get it from struts

 

3. All business actions are configured as DelegatingActionProxy class

 

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 rewrites the execute method. When struts calls the execute method, it will internally call wac.getBean("prefix+path") to obtain the Action object managed by Spring, and then call the execute method of the business Action object. Proxy mode to proxy Action

 

Spring configuration file:

 

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>

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326341623&siteId=291194637