Spring 和struts 整合的三种方式 - 曹映辉的专栏 - CSDN博客
来源:百度文库 编辑:神马文学网 时间:2024/06/13 11:57:26
Spring 和struts 整合的三种方式 收藏
Spring 和 struts 整合的三种方式。1,使用Spring 的 ActionSupport
2, 使用Spring 的 DelegatingRequestProcessor 类。
3,全权委托。无论用那种方法来整合第一步就是要为struts来装载spring的应用环境。 就是在 struts 中加入一个插件。
struts-config.xml中
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 中的配置
这中配置方式同直接在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 中 通过 来替代
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 中的配置。
这里 name="/login" 与struts 中的path匹配
class="com.cao.struts.action.LoginAction" 与struts 中的type匹配 还要为 LoginAction 提供必要的setXXX方法。 获得ApplicationCotext和依赖注入的工作都在DelegatingRequestProcessor中完成。
3,全权委托:
Action 的创建和对象的依赖注入全部由IOC容器来完成。 使用Spring的DelegatingAcionProxy来帮助实现代理的工作
org.springframework.web.struts.DelegatingActiongProxy继承于org.apache.struts.action.Action .
全权委托的配置方式同 方式 2 类似 (applcationContext.xml文件的配置和 Action类的实现方式相同)。
attribute="loginForm"
input="login.jsp"
name="loginForm"
path="/login"
scope="request" type="org.springframework.web.struts.DelegatingActionProxy" >
不同之处
1,中 type指向的是spring 的代理类2, 去掉struts-config.xml中 三种整和方式中我们优先选用 全权委托的方式。
理由:
1,第一种使得过多的耦合了Spring和Action .
2,RequestProcessor类已经被代理 如果要再实现自己的实现方式(如:编码处理)怕有点麻烦。总结一下:
整合工作中的步骤:
1,修改struts-config.xml
2, 配置applicationContext.xml
3, 为Action添加get/set方法 来获得依赖注入的功能。 本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/caoyinghui1986/archive/2008/06/02/2501914.aspx
Spring 和 struts 整合的三种方式。1,使用Spring 的 ActionSupport
2, 使用Spring 的 DelegatingRequestProcessor 类。
3,全权委托。无论用那种方法来整合第一步就是要为struts来装载spring的应用环境。 就是在 struts 中加入一个插件。
struts-config.xml中
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 中的配置
但实现了表示层和业务逻辑层的解耦(LoginDao dao = (LoginDao) ctx.getBean("loginDao"))。
2,使用Spring 的 DelegatingRequestProcessor 类
DelegatingRequestProcessor 继承自 org.apache.struts.action.RequestProcessor 并覆盖了里面的方法。
sturts-config.xml 中
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 中的配置。
class="com.cao.struts.action.LoginAction" 与struts 中的type匹配 还要为 LoginAction 提供必要的setXXX方法。 获得ApplicationCotext和依赖注入的工作都在DelegatingRequestProcessor中完成。
3,全权委托:
Action 的创建和对象的依赖注入全部由IOC容器来完成。 使用Spring的DelegatingAcionProxy来帮助实现代理的工作
org.springframework.web.struts.DelegatingActiongProxy继承于org.apache.struts.action.Action .
全权委托的配置方式同 方式 2 类似 (applcationContext.xml文件的配置和 Action类的实现方式相同)。
input="login.jsp"
name="loginForm"
path="/login"
scope="request" type="org.springframework.web.struts.DelegatingActionProxy" >
不同之处
1,
理由:
1,第一种使得过多的耦合了Spring和Action .
2,RequestProcessor类已经被代理 如果要再实现自己的实现方式(如:编码处理)怕有点麻烦。总结一下:
整合工作中的步骤:
1,修改struts-config.xml
2, 配置applicationContext.xml
3, 为Action添加get/set方法 来获得依赖注入的功能。 本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/caoyinghui1986/archive/2008/06/02/2501914.aspx
Spring 和struts 整合的三种方式 - 曹映辉的专栏 - CSDN博客
用 Spring 更好地处理 Struts 动作三种整合 Struts 应用程序与 Spring 的方式
使用 Spring 更好地处理 Struts 动作三种整合 Struts 应用程序与 Spring 的方式
Spring , Struts整合方法 - 一侠 的 专栏 - CSDNBlog
C#实现启用、禁用本地网络的三种方式 - dsd999的专栏 - CSDN博客
C#实现启用、禁用本地网络的三种方式 - dsd999的专栏 - CSDN博客
Struts and Spring的整合总结
spring Lookup方法注入 - qking93415981的专栏 - CSDN博客
tomcat5.5 整合 iis6.0 - shangpusp的专栏 - CSDN博客
spring和ibatis的整合
8. 4 常见的架构设计策略 - 《轻量级J2EE企业应用实战——Struts Spring Hibernate整合开发》 - 免费试读 - book.csdn.net
baozhengw的专栏 - CSDN博客
spring和ibatis的整合1
spring和ibatis的整合2
继承与混合,略谈系统的构建方式 - aimingoo的专栏 - CSDN博客
Struts Spring Hibernate整合笔记
Struts Spring Hibernate整合笔记
Struts+Spring+Hibernate的技術實現
浮点数在计算机中存储方式 - virus026的专栏 - CSDN博客
ssh项目中集成spring的三种方式
Struts 2, spring 2, hibernate 的整合-Java频道-中国IT...
AGPS简介 - kv110的专栏 - CSDN博客
OpenMAX简介 - shenbin1430的专栏 - CSDN博客
Android flinger - simmer_ken的专栏 - CSDN博客