struts+spring的整合方案,在图书馆看书摘下来的
(1)利用spring提供的ActionSupport
ActionSupport
DispatchActionSupport
LookupDispatchActionSupport
MappingDispathActionSupport
在Action类中,需要编写自己获取bean的代码
privateValidBeanvb;
publicValidBeangetVb()
{
return(ValidBean)getWebApplicationContext().getBean("vb");
}
在Action类中,该Action类没有交给IOC委托处理,只是把部分的私有成员交给spring处理
把代码与spring耦合起来,这个是最方便的方法,有利于理解,但是造成代码污染。
但对原来的代码污染最少。
(2)DelegatingActionProxy
主要涉及几个方面的修改
在struts的配置文件中
<form-beans>
<form-beanname="loginForm"type="scut.LoginForm">
</form-beans>
<action-mappings>
<actionpath="/login"type="org.springframework.web.struts.DelegatingActionProxy"name="loginForm"scope="request"validate="true"input="/login.jsp">
<forwardname="input"path="/login.jsp"/>
<forwardname="welcome"path="/welcome.html"/>
</action>
</action-mapping>
//所有的action都是指向同一个的处理代理类,看起来会比较让人迷惑
<plug-inclassname="org.springframework.web.struts.ContextLoaderPlugIn">
<set-propertyproperty="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
<?xmlversion="1.0"encoding="gb2312"?>
<!DOCTYPEbeansPUBLIC"-//SPRING//DTDBEWAN//EN"
"http://www.springframework.org/dtd/spring-beans.dtd>
<beans>
<beanname="/login"class="scut.LoginAction"singleton="false">
<propertyname="vb">
<refbean="vb">
</property>
</bean>
</beans>
action-servlet.xml
<?xmlversion="1.0"encoding="gb2312"?>
<!DOCTYPEbeansPUBLIC"-//SPRING//DTDBEWAN//EN"
"http://www.springframework.org/dtd/spring-beans.dtd>
<beans>
<beanid="vb"class="scut.ValidBeanImpl"/>
</bean>
</beans>
同时Action类要有少量的修改
添加注入方法
privateValidBeanvb
publicvoidsetVb(ValidBeanvb)
{
this.vb=vb;
}
//这个是一种面向接口的编成,实现类是ValidBeanImpl的实例,注入到ValidBeanvb中,由于
它实现了该接口,所以不会存在问题。
(3)使用DelegatingRequestProcessor
<action-mappings>
<actionpath="/login"name="loginForm"scope="request"validate="true"input="/login.jsp">
<forwardname="input"path="/login.jsp"/>
<forwardname="welcome"path="/welcome.html"/>
</action>
</action-mapping>
//所有的action都没有指定一个type的处理
<controllerprocessorclass="org.springframwork.web.struts.DelegatingRequestProcessor"/>
//使用了DelegatingRequestProcessor来代替原来struts的RequestProcessor
<plug-inclassname="org.springframework.web.struts.ContextLoaderPlugIn">
<set-propertyproperty="contextConfigLocation"
value="/WEB-INF/applicationContext.xml",
/WEB-INF/action-serlet.xml"/>
</plug-in>
packagecom.ericsson.bmf.portal.web.action;
importjavax.servlet.http.HttpServletRequest;
importjavax.servlet.http.HttpServletResponse;
importorg.apache.struts.action.Action;
importorg.apache.struts.action.ActionForm;
importorg.apache.struts.action.ActionForward;
importorg.apache.struts.action.ActionMapping;
importorg.apache.struts.action.ActionServlet;
importorg.apache.struts.actions.MappingDispatchAction;
importorg.apache.struts.config.ModuleConfig;
importorg.springframework.beans.BeansException;
importorg.springframework.web.context.WebApplicationContext;
importorg.springframework.web.struts.ContextLoaderPlugIn;
importorg.springframework.web.struts.DelegatingActionUtils;
/**
*Reconstructspring's<code>DelegatingActionProxy</code>tosupportMappingDispathAction.Forexample:
*
*<p>TheproxyisdefinedintheStrutsconfigfile,specifyingthis
*classasactionclass.ItwilldelegatetoaStrutsActionbean
*intheContextLoaderPlugIncontext.
*
*<pre>
*<actionpath="/login"type="com.ericsson.cgc.common.portal.web.action.BaseActionProxy"/></pre>
*
*ThenameoftheActionbeanintheWebApplicationContextwillbe
*determinedfromthemappingpathandmoduleprefix.Thiscanbe
*customizedbyoverridingthe<code>determineActionBeanName</code>method.
*
*<p>Example:
*<ul>
*<li>mappingpath"com.ericsson.cgc.common.portal.web.action.BaseActionProxy"->beanname"com.ericsson.cgc.common.portal.web.action.BaseActionProxy"<br>
*<li>mappingpath"com.ericsson.cgc.common.portal.web.action.BaseActionProxy",moduleprefix"/mymodule"->
*beanname"/mymodule/com.ericsson.cgc.common.portal.web.action.BaseActionProxy"
*</ul>
*
*<p>AcorrespondingbeandefinitionintheContextLoaderPlugin
*contextlooksasfollows,beingabletofullyleverage
*Spring'sconfigurationfacilities:
*
*<pre>
*<beanname="com.ericsson.cgc.common.portal.web.action.BaseActionProxy"class="myapp.MyAction">
*<propertyname="...">...</property>
*</bean></pre>
*
*
*
*@authorezenwan
*
*/
publicclassBaseActionProxyextendsBaseAction{
/**
*PasstheexecutecallontotheSpring-manageddelegateAction.
*@see#getDelegateAction
*/
publicActionForwardexecute(
ActionMappingmapping,ActionFormform,HttpServletRequestrequest,HttpServletResponseresponse)
throwsException{
BaseActionProxydelegateAction=(BaseActionProxy)getDelegateAction(mapping);
returndelegateAction.executeAction(mapping,form,request,response);
}
publicActionForwardexecuteAction(
ActionMappingmapping,ActionFormform,HttpServletRequestrequest,HttpServletResponseresponse)
throwsException{
returnsuper.execute(mapping,form,request,response);
}
/**
*ReturnthedelegateActionforthegivenmapping.
*<p>Thedefaultimplementationdeterminesabeannamefromthe
*givenActionMappingandlooksupthecorrespondingbeaninthe
*WebApplicationContext.
*@parammappingtheStrutsActionMapping
*@returnthedelegateAction
*@throwsBeansExceptionifthrownbyWebApplicationContextmethods
*@see#determineActionBeanName
*/
protectedActiongetDelegateAction(ActionMappingmapping)throwsBeansException{
WebApplicationContextwac=getWebApplicationContext(getServlet(),mapping.getModuleConfig());
StringbeanName=determineActionBeanName(mapping);
return(Action)wac.getBean(beanName,Action.class);
}
/**
*FetchContextLoaderPlugIn'sWebApplicationContextfromtheServletContext,
*fallingbacktotherootWebApplicationContext.Thiscontextissupposed
*tocontaintheStrutsActionbeanstodelegateto.
*@paramactionServlettheassociatedActionServlet
*@parammoduleConfigtheassociatedModuleConfig
*@returntheWebApplicationContext
*@throwsIllegalStateExceptionifnoWebApplicationContextcouldbefound
*@seeDelegatingActionUtils#findRequiredWebApplicationContext
*@seeContextLoaderPlugIn#SERVLET_CONTEXT_PREFIX
*/
protectedWebApplicationContextgetWebApplicationContext(
ActionServletactionServlet,ModuleConfigmoduleConfig)throwsIllegalStateException{
returnDelegatingActionUtils.findRequiredWebApplicationContext(actionServlet,moduleConfig);
}
/**
*DeterminethenameoftheActionbean,tobelookedupin
*theWebApplicationContext.
*<p>Thedefaultimplementationtakesthemappingtype
*
*@parammappingtheStrutsActionMapping
*@returnthenameoftheActionbean
*@seeorg.apache.struts.action.ActionMapping#getType
*/
protectedStringdetermineActionBeanName(ActionMappingmapping){
Stringtype=mapping.getType();
StringbeanName=type;
returnbeanName;
}
}