struts + spring 的整合方案

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;
}

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值