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;
}
}
分享到:
相关推荐
struts1+spring+hibernate+DWR整合方案详解
轻量级 J2EE 企业应用实战:Struts+Spring+Hibernate 整合开发
Spring+Struts+Hibernate比较详细的整合配置方案
MyEclipse7.5+flex4+spring3.0.5+struts2.2.1+hibernate3.6.0+blazeds4.0.0.14931完美整合方案 内附所需jar包和源代码
Struts2+spring2+hibernate3整合方案
struts2+hibernate+spring整合开发+分页方案
精通J2EE--Eclipse、Struts、Hibernate及Spring整合应用案例
2、 Spring配置提供了多种可选整合方案:详见applicationContext.xml的配置 (1) 提供支持三种数据源方案:C3P0、DBCP、JNDI,三选一,请注释或删除其他方案 (2) 提取四种Hibernate整合方案:四选一,请注释或...
"会员管理系统(struts+hibernate+spring)130226.rar" 是一个针对计算机专业的JSP源码资料包,它集成了Struts、Hibernate和Spring框架,旨在为用户提供一个高效、稳定且易于维护的会员管理解决方案。这个系统通过使用...
Struts2+Sprint2+Hibernate3整合方案(源代码) 博文链接:https://hnylj.iteye.com/blog/160624
Spring+Struts+Hibernate比较详细的整合配置方案.pdf
基于Struts+Hibernate+Spring的整合架构及其在Web开发中的应用.pdf 基于Struts+Spring+Hibernate架构的轻量级J2EE的研究与应用.pdf 基于Struts+Spring+Hibernate架构的进销存管理系统的设计与实现.pdf 基于...
Spring框架则以其依赖注入和面向切面编程的特性,优雅地管理了应用程序中的复杂性,如事务处理、安全性以及与多种持久层技术的整合。Hibernate则是项目中的ORM(对象关系映射)解决方案,它将Java对象映射到Oracle...