struts + spring 的整合方案,在图书馆看书摘下来的
(1)利用spring 提供的ActionSupport
ActionSupport
DispatchActionSupport
LookupDispatchActionSupport
MappingDispathActionSupport
在Action类中,需要编写自己获取bean的代码
private ValidBean vb;
public ValidBean getVb()
{
return (ValidBean) getWebApplicationContext().getBean("vb");
}
在Action类中,该Action类没有交给 IOC委托处理,只是把部分的私有成员交给spring处理
把代码与spring耦合起来,这个是最方便的方法,有利于理解,但是造成代码污染。
但对原来的代码污染最少。
(2)DelegatingActionProxy
主要涉及几个方面的修改
在struts的配置文件中
<form-beans>
<form-bean name="loginForm" type="scut.LoginForm">
</form-beans>
<action-mappings>
<action path="/login" type="org.springframework.web.struts.DelegatingActionProxy" name="loginForm" scope="request" validate="true" input="/login.jsp">
<forward name="input" path="/login.jsp"/>
<forward name="welcome" path="/welcome.html" />
</action>
</action-mapping>
//所有的action都是指向同一个的处理代理类,看起来会比较让人迷惑
<plug-in className="org.springframework.web.struts.ContextLoaderPlugIn">
<set-property property="contextConfigLocation"
value="/WEB-INF/applicationContext.xml",
/WEB-INF/action-serlet.xml"/>
</plug-in>
//以plug-in的形式引入spring框架,并且给出多个配置文件
//action-servlet.xml用来配置表现层的context 就是把Action作为一个bean处理
//applicationContext.xml 通常是配置其它的私有bean
action-servlet.xml
<?xml version="1.0" encoding="gb2312"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEWAN//EN"
"http://www.springframework.org/dtd/spring-beans.dtd>
<beans>
<bean name="/login" class="scut.LoginAction" singleton="false">
<property name="vb">
<ref bean="vb">
</property>
</bean>
</beans>
action-servlet.xml
<?xml version="1.0" encoding="gb2312"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEWAN//EN"
"http://www.springframework.org/dtd/spring-beans.dtd>
<beans>
<bean id="vb" class="scut.ValidBeanImpl"/>
</bean>
</beans>
同时 Action类要有少量的修改
添加注入方法
private ValidBean vb
public void setVb(ValidBean vb)
{
this.vb=vb;
}
//这个是一种面向接口的编成,实现类是ValidBeanImpl的实例,注入到ValidBean vb中,由于
它实现了该接口,所以不会存在问题。
(3)使用DelegatingRequestProcessor
<action-mappings>
<action path="/login" name="loginForm" scope="request" validate="true" input="/login.jsp">
<forward name="input" path="/login.jsp"/>
<forward name="welcome" path="/welcome.html" />
</action>
</action-mapping>
//所有的action都没有指定一个type的处理
<controller processorClass="org.springframwork.web.struts.DelegatingRequestProcessor"/>
//使用了DelegatingRequestProcessor来代替原来struts的RequestProcessor
<plug-in className="org.springframework.web.struts.ContextLoaderPlugIn">
<set-property property="contextConfigLocation"
value="/WEB-INF/applicationContext.xml",
/WEB-INF/action-serlet.xml"/>
</plug-in>
package com.ericsson.bmf.portal.web.action;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionServlet;
import org.apache.struts.actions.MappingDispatchAction;
import org.apache.struts.config.ModuleConfig;
import org.springframework.beans.BeansException;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.struts.ContextLoaderPlugIn;
import org.springframework.web.struts.DelegatingActionUtils;
/**
* Reconstruct spring's <code>DelegatingActionProxy</code> to support MappingDispathAction. For example:
*
* <p>The proxy is defined in the Struts config file, specifying this
* class as action class. It will delegate to a Struts Action bean
* in the ContextLoaderPlugIn context.
*
* <pre>
* <action path="/login" type="com.ericsson.cgc.common.portal.web.action.BaseActionProxy"/></pre>
*
* The name of the Action bean in the WebApplicationContext will be
* determined from the mapping path and module prefix. This can be
* customized by overriding the <code>determineActionBeanName</code> method.
*
* <p>Example:
* <ul>
* <li>mapping path "com.ericsson.cgc.common.portal.web.action.BaseActionProxy" -> bean name "com.ericsson.cgc.common.portal.web.action.BaseActionProxy"<br>
* <li>mapping path "com.ericsson.cgc.common.portal.web.action.BaseActionProxy", module prefix "/mymodule" ->
* bean name "/mymodule/com.ericsson.cgc.common.portal.web.action.BaseActionProxy"
* </ul>
*
* <p>A corresponding bean definition in the ContextLoaderPlugin
* context looks as follows, being able to fully leverage
* Spring's configuration facilities:
*
* <pre>
* <bean name="com.ericsson.cgc.common.portal.web.action.BaseActionProxy" class="myapp.MyAction">
* <property name="...">...</property>
* </bean></pre>
*
*
*
* @author ezenwan
*
*/
public class BaseActionProxy extends BaseAction {
/**
* Pass the execute call on to the Spring-managed delegate Action.
* @see #getDelegateAction
*/
public ActionForward execute(
ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)
throws Exception {
BaseActionProxy delegateAction = (BaseActionProxy)getDelegateAction(mapping);
return delegateAction.executeAction(mapping, form, request, response);
}
public ActionForward executeAction(
ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)
throws Exception {
return super.execute(mapping, form, request, response);
}
/**
* Return the delegate Action for the given mapping.
* <p>The default implementation determines a bean name from the
* given ActionMapping and looks up the corresponding bean in the
* WebApplicationContext.
* @param mapping the Struts ActionMapping
* @return the delegate Action
* @throws BeansException if thrown by WebApplicationContext methods
* @see #determineActionBeanName
*/
protected Action getDelegateAction(ActionMapping mapping) throws BeansException {
WebApplicationContext wac = getWebApplicationContext(getServlet(), mapping.getModuleConfig());
String beanName = determineActionBeanName(mapping);
return (Action) wac.getBean(beanName, Action.class);
}
/**
* Fetch ContextLoaderPlugIn's WebApplicationContext from the ServletContext,
* falling back to the root WebApplicationContext. This context is supposed
* to contain the Struts Action beans to delegate to.
* @param actionServlet the associated ActionServlet
* @param moduleConfig the associated ModuleConfig
* @return the WebApplicationContext
* @throws IllegalStateException if no WebApplicationContext could be found
* @see DelegatingActionUtils#findRequiredWebApplicationContext
* @see ContextLoaderPlugIn#SERVLET_CONTEXT_PREFIX
*/
protected WebApplicationContext getWebApplicationContext(
ActionServlet actionServlet, ModuleConfig moduleConfig) throws IllegalStateException {
return DelegatingActionUtils.findRequiredWebApplicationContext(actionServlet, moduleConfig);
}
/**
* Determine the name of the Action bean, to be looked up in
* the WebApplicationContext.
* <p>The default implementation takes the mapping type
*
* @param mapping the Struts ActionMapping
* @return the name of the Action bean
* @see org.apache.struts.action.ActionMapping#getType
*/
protected String determineActionBeanName(ActionMapping mapping) {
String type = mapping.getType();
String beanName = type;
return beanName;
}
}
(1)利用spring 提供的ActionSupport
ActionSupport
DispatchActionSupport
LookupDispatchActionSupport
MappingDispathActionSupport
在Action类中,需要编写自己获取bean的代码
private ValidBean vb;
public ValidBean getVb()
{
return (ValidBean) getWebApplicationContext().getBean("vb");
}
在Action类中,该Action类没有交给 IOC委托处理,只是把部分的私有成员交给spring处理
把代码与spring耦合起来,这个是最方便的方法,有利于理解,但是造成代码污染。
但对原来的代码污染最少。
(2)DelegatingActionProxy
主要涉及几个方面的修改
在struts的配置文件中
<form-beans>
<form-bean name="loginForm" type="scut.LoginForm">
</form-beans>
<action-mappings>
<action path="/login" type="org.springframework.web.struts.DelegatingActionProxy" name="loginForm" scope="request" validate="true" input="/login.jsp">
<forward name="input" path="/login.jsp"/>
<forward name="welcome" path="/welcome.html" />
</action>
</action-mapping>
//所有的action都是指向同一个的处理代理类,看起来会比较让人迷惑
<plug-in className="org.springframework.web.struts.ContextLoaderPlugIn">
<set-property property="contextConfigLocation"
value="/WEB-INF/applicationContext.xml",
/WEB-INF/action-serlet.xml"/>
</plug-in>
//以plug-in的形式引入spring框架,并且给出多个配置文件
//action-servlet.xml用来配置表现层的context 就是把Action作为一个bean处理
//applicationContext.xml 通常是配置其它的私有bean
action-servlet.xml
<?xml version="1.0" encoding="gb2312"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEWAN//EN"
"http://www.springframework.org/dtd/spring-beans.dtd>
<beans>
<bean name="/login" class="scut.LoginAction" singleton="false">
<property name="vb">
<ref bean="vb">
</property>
</bean>
</beans>
action-servlet.xml
<?xml version="1.0" encoding="gb2312"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEWAN//EN"
"http://www.springframework.org/dtd/spring-beans.dtd>
<beans>
<bean id="vb" class="scut.ValidBeanImpl"/>
</bean>
</beans>
同时 Action类要有少量的修改
添加注入方法
private ValidBean vb
public void setVb(ValidBean vb)
{
this.vb=vb;
}
//这个是一种面向接口的编成,实现类是ValidBeanImpl的实例,注入到ValidBean vb中,由于
它实现了该接口,所以不会存在问题。
(3)使用DelegatingRequestProcessor
<action-mappings>
<action path="/login" name="loginForm" scope="request" validate="true" input="/login.jsp">
<forward name="input" path="/login.jsp"/>
<forward name="welcome" path="/welcome.html" />
</action>
</action-mapping>
//所有的action都没有指定一个type的处理
<controller processorClass="org.springframwork.web.struts.DelegatingRequestProcessor"/>
//使用了DelegatingRequestProcessor来代替原来struts的RequestProcessor
<plug-in className="org.springframework.web.struts.ContextLoaderPlugIn">
<set-property property="contextConfigLocation"
value="/WEB-INF/applicationContext.xml",
/WEB-INF/action-serlet.xml"/>
</plug-in>
package com.ericsson.bmf.portal.web.action;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionServlet;
import org.apache.struts.actions.MappingDispatchAction;
import org.apache.struts.config.ModuleConfig;
import org.springframework.beans.BeansException;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.struts.ContextLoaderPlugIn;
import org.springframework.web.struts.DelegatingActionUtils;
/**
* Reconstruct spring's <code>DelegatingActionProxy</code> to support MappingDispathAction. For example:
*
* <p>The proxy is defined in the Struts config file, specifying this
* class as action class. It will delegate to a Struts Action bean
* in the ContextLoaderPlugIn context.
*
* <pre>
* <action path="/login" type="com.ericsson.cgc.common.portal.web.action.BaseActionProxy"/></pre>
*
* The name of the Action bean in the WebApplicationContext will be
* determined from the mapping path and module prefix. This can be
* customized by overriding the <code>determineActionBeanName</code> method.
*
* <p>Example:
* <ul>
* <li>mapping path "com.ericsson.cgc.common.portal.web.action.BaseActionProxy" -> bean name "com.ericsson.cgc.common.portal.web.action.BaseActionProxy"<br>
* <li>mapping path "com.ericsson.cgc.common.portal.web.action.BaseActionProxy", module prefix "/mymodule" ->
* bean name "/mymodule/com.ericsson.cgc.common.portal.web.action.BaseActionProxy"
* </ul>
*
* <p>A corresponding bean definition in the ContextLoaderPlugin
* context looks as follows, being able to fully leverage
* Spring's configuration facilities:
*
* <pre>
* <bean name="com.ericsson.cgc.common.portal.web.action.BaseActionProxy" class="myapp.MyAction">
* <property name="...">...</property>
* </bean></pre>
*
*
*
* @author ezenwan
*
*/
public class BaseActionProxy extends BaseAction {
/**
* Pass the execute call on to the Spring-managed delegate Action.
* @see #getDelegateAction
*/
public ActionForward execute(
ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)
throws Exception {
BaseActionProxy delegateAction = (BaseActionProxy)getDelegateAction(mapping);
return delegateAction.executeAction(mapping, form, request, response);
}
public ActionForward executeAction(
ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)
throws Exception {
return super.execute(mapping, form, request, response);
}
/**
* Return the delegate Action for the given mapping.
* <p>The default implementation determines a bean name from the
* given ActionMapping and looks up the corresponding bean in the
* WebApplicationContext.
* @param mapping the Struts ActionMapping
* @return the delegate Action
* @throws BeansException if thrown by WebApplicationContext methods
* @see #determineActionBeanName
*/
protected Action getDelegateAction(ActionMapping mapping) throws BeansException {
WebApplicationContext wac = getWebApplicationContext(getServlet(), mapping.getModuleConfig());
String beanName = determineActionBeanName(mapping);
return (Action) wac.getBean(beanName, Action.class);
}
/**
* Fetch ContextLoaderPlugIn's WebApplicationContext from the ServletContext,
* falling back to the root WebApplicationContext. This context is supposed
* to contain the Struts Action beans to delegate to.
* @param actionServlet the associated ActionServlet
* @param moduleConfig the associated ModuleConfig
* @return the WebApplicationContext
* @throws IllegalStateException if no WebApplicationContext could be found
* @see DelegatingActionUtils#findRequiredWebApplicationContext
* @see ContextLoaderPlugIn#SERVLET_CONTEXT_PREFIX
*/
protected WebApplicationContext getWebApplicationContext(
ActionServlet actionServlet, ModuleConfig moduleConfig) throws IllegalStateException {
return DelegatingActionUtils.findRequiredWebApplicationContext(actionServlet, moduleConfig);
}
/**
* Determine the name of the Action bean, to be looked up in
* the WebApplicationContext.
* <p>The default implementation takes the mapping type
*
* @param mapping the Struts ActionMapping
* @return the name of the Action bean
* @see org.apache.struts.action.ActionMapping#getType
*/
protected String determineActionBeanName(ActionMapping mapping) {
String type = mapping.getType();
String beanName = type;
return beanName;
}
}