一.用spring的DelegatingRequestProcessor替换struts的RequestProcessor.
1)不要在web.xml中设置ApplicationContext的自动加载,在struts-config.xml中通plug-in设置.
<plug-in className="org.springframework.web.struts.ContextLoaderPlugIn">
<set-property value="/WEB-INF/applicationContext.xml,/WEB-INF/appContext.xml" property="contextConfigLocation" />
</plug-in>
2)在struts-config.xml中设置RequestProcessor的替换类.
<controller processorClass="org.springframework.web.struts.DelegatingRequestProcessor"></controller>
3)不要在struts-config.xml中的<action>元素中设置action的type属性.
<action path="/login" input="/index.jsp"
validate="true" scope="request">
<forward name="forward" path="/success.jsp"></forward>
</action>
4)在applicationContext.xml或其他spring bean配置文件中设置由DelegatingRequestProcessor转发的bean,这个bean就是Action类.
<bean name="/login" class="mypack.LoginAction" singleton="false">
<property name="property1" ref="otherbean"/>
</bean>
二.使用DelegatingActionProxy,此种方法是在action中再把请求转发给定义在applicationContext.xml中的Action.
1)同第一种方法的1).
2)如果试了第一种方法,去掉struts-config.xml中的
<controller processorClass="org.springframework.web.struts.DelegatingRequestProcessor"></controller>
元素.
3)需要在struts-config.xml中定义action的type="org.springframework.web.struts.DelegatingActionProxy".即第一种方法的3)中加入type属性.
<action path="/login" input="/index.jsp" validate="true"
scope="request" type="org.springframework.web.struts.DelegatingActionProxy">
<forward name="forward" path="/success.jsp"></forward>
</action>
4)同第一种方法的4).
三.使用Spring的ActionSupport .
Spring 的ActionSupport 继承至org.apache.struts.action.Action
ActionSupport的子类可以或得 WebApplicationContext类型的全局变量。通过getWebApplicationContext()可以获得这个变量。
这是一个 servlet 的代码:
public class LoginAction extends org.springframework.web.struts.ActionSupport {
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) {
LoginForm loginForm = (LoginForm) form;// TODO Auto-generated method stub
//获得 WebApplicationContext 对象
WebApplicationContext ctx = this.getWebApplicationContext();
LoginDao dao = (LoginDao) ctx.getBean("loginDao");
User u = new User();
u.setName(loginForm.getName());
u.setPwd(loginForm.getPwd());
if(dao.checkLogin(u)){
return mapping.findForward("success");
}else{
return mapping.findForward("error");
}
}
}
applicationContext.xml 中的配置
<beans>
<bean id="loginDao" class="com.cao.dao.LoginDao"/>
</beans>
|
这中配置方式同直接在web.xml文件配置差别不大。
注意:Action继承自 org.springframework.web.struts.ActionSupport 使得struts和spring耦合在一起。
但实现了表示层和业务逻辑层的解耦(LoginDao dao = (LoginDao) ctx.getBean("loginDao"))。
分享到:
相关推荐
博文链接:https://qqwyy.iteye.com/blog/181991
Spring2.0对Struts2.0的整合,内含一个小示例,带清晰注释
Spring2.5.3+Struts2.0.11.1+Hibernate3.2.6整合备忘 (转载)
Struts2.Hibernate3.2.Spring2.0整合 功能很齐全
Struts2.Hibernate3.2.Spring2.0整合项目代码(包括增删查改功能) jar在:(Struts2.Hibernate3.2.Spring2.0整合项目代码的jar包) 里面下载
Struts2.Hibernate3.2.Spring2.0整合项目代码的jar包 Struts2.Hibernate3.2.Spring2.0整合项目代码的jar包
Struts 2.0 + Hibernate 3.2 + Spring 2.0 框架整合.doc
对spring 2.0 + struts 2.0 + hibernate 3.2 进行了系统的整合。
总结,该例示注释非常详细,演示Spring托管Hibernate和Struts的Action, 以及Spring的事务声明(包括1.x与2.x的用法)与非事务声明的使用方式。 使用DWR可以方便的使用Spring托管的持久层功能。 目的:希望广大Java...
Struts2 Hibernate3.2 Spring2.0 整合 J2EE SSH
以前整合大多数为struts1.x hibernate spring 现在struts2.x也出来一阵子了,大多公司开始使用struts2.x 所以这就造就了整合问题,此系统能帮助初学都迅速进入状态,迅速上手
struts2.0jar包以及struts2.0与spring的整合jar包
spring2.5.6 整合struts2.0 spring2.5.6 整合struts2.0 spring2.5.6 整合struts2.0
struts2.0 hibernate spring 整合S2SH
Struts2.0-Hibernate3.2-Spring2.0整合.doc
Ibatis Struts1.2 Spring 2.0 整合终极版,Ibatis Struts1.2 Spring 2.0 整合终极版,Ibatis Struts1.2 Spring 2.0 整合终极版