首页 ┆ 网站地图 ┆ 在线留言 ┆ 游戏资讯 ┆ 资源下载 ┆ 端午节祝福 ┆ 迅雷在线影视 ┆淘宝手机在线充值 ┆淘宝游戏点卡充值 
设为首页
加入收藏
联系我们
高级搜索
您当前的位置: 主页>JAVA专区>SPRING>实例讲解spring整合struts的几种方式
实例讲解spring整合struts的几种方式
来源: 发布时间:2008-06-02 发布人: 浏览: 人次   字体: [ ]  

实例讲解spring整合struts的几种方式
1,使用Spring 的 ActionSupport
2, 使用Spring 的 DelegatingRequestProcessor 类。
3,全权委托。

无论用那种方法来整合第一步就是要为struts来装载spring的应用环境。 就是在 struts 中加入一个插件。
struts-config.xml中

 <plug-in className="org.springframework.web.struts.ContextLoaderPlugIn">
  <set-property property="contextConfigLocation" value="/WEB-INF/applicationContext.xml"/>
 </plug-in>

spring 的配置文件被作为参数配置进来。这样可以省略对web.xml 文件中的配置。确保你的applicationContext.xml 在WEB-INF目录下面

1,使用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"))。

2,使用Spring 的 DelegatingRequestProcessor 类
DelegatingRequestProcessor  继承自 org.apache.struts.action.RequestProcessor 并覆盖了里面的方法。
sturts-config.xml  中  <controller processorClass="org.springframework.web.struts.DelegatingRequestProcessor"/> 通过 <controller >来替代
                        org.apache.struts.action.RequestProcessor 的请求处理。

public class LoginAction extends Action {
 //利用spring来注入这个对象。
 private LoginDao dao ;
 
 public void setDao(LoginDao dao) {
  System.out.println("执行注入");
  this.dao = dao;
 }

 public LoginDao getDao() {
  return dao;
 }

 public ActionForward execute(ActionMapping mapping, ActionForm form,
   HttpServletRequest request, HttpServletResponse response) {
  LoginForm loginForm = (LoginForm) form;// TODO Auto-generated method stub
  //这样一改这行代码似乎没有必要了。
  //WebApplicationContext ctx = this.getWebApplicationContext();
  //LoginDao dao = (LoginDao) ctx.getBean("loginDao");
  User u = new User(); 
  u.setName(loginForm.getName());
  u.setPwd(loginForm.getPwd()); 
  //直接用dao来调用spring会将这个对象实例化。
  if(dao.checkLogin(u)){
   return mapping.findForward("success");
  }else{
   return  mapping.findForward("error");
  } 
 }
}
这里的。
LoginAction extends Action 说明 struts 每有和spring 耦合。
看一下
applicationContext.xml 中的配置。
<beans>
 <bean id="loginDao" class="com.cao.dao.LoginDao"/>
 <bean name="/login" class="com.cao.struts.action.LoginAction">
  <property name="dao">
   <ref local="loginDao"/>
  </property>
 </bean>
</beans>

这里 name="/login" 与struts 中的path匹配 class="com.cao.struts.action.LoginAction" 与struts 中的type匹配还要为 LoginAction 提供必要的setXXX方法。 获得ApplicationCotext和依赖注入的工作都在DelegatingRequestProcessor中完成。


共2页: 上一页 1 [2] 下一页
相 关 文 章   发布商链接
·java中利用spring动态的创建hibernat...
·Quartz的配置及使用和定时触发功能的...
·使用Spring集成XFire开发WebServic...
·Spring jar包详解
·为什么Spring成为SOA开发的首选Java...
·Spring框架简介及其Spring事务管理应...
 §最新评论:(评论内容只代表网友观点,与本站立场无关!)
网名: 验证码:  【所有评论】【↑返回顶部
评 分: 12 345
评论内容:(不能超过500字,请自觉遵守互联网相关政策法规。[按 Ctrl+Enter 可直接提交]
注意:请勿在本站发布政治话题、色情及违反法律的内容。
IT知道网 声明:刊登此文章是为了传递更多信息,文章内容仅供参考,转载请注明出处。
推 荐 文 章
·Quartz的配置及使用和定时触...
·java中利用spring动态的创建h...
·Spring框架简介及其Spring事...
·为什么Spring成为SOA开发的首
·Spring jar包详解
·使用Spring集成XFire开发WebS...
热 门 文 章
·java中利用spring动态的创建h...
·Spring框架简介及其Spring事...
·Spring jar包详解
·为什么Spring成为SOA开发的首...
·Quartz的配置及使用和定时触...
·使用Spring集成XFire开发WebS...
网站首页 - 关于本站 - 加入收藏 - 网站地图 - 友情连接 - 在线留言 - 联系我们 - 返回顶部
Copyright © 2007 IT知道网.[冀ICP备07026896号]. All Rights Reserved .