整合Spring与Struts1的三种方法总结
2016-10-24 00:00
501 查看
摘要:整合Spring与Struts1的三种方法总结
10th05-2012分类:编程代码浏览(10,572)评论(1)
1.使用Spring的ActionSupport。
2.使用Spring的DelegatingRequestProcessor类。
3.全权委托。
无论用那种方法来整合第一步就是要为struts来装载spring的应用环境。就是在struts中加入一个插件。struts-config.xml中
<plug-inclassName=”org.springframework.web.struts.ContextLoaderPlugIn”>
<set-propertyproperty=”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的代码:
publicclassLoginActionextendsorg.springframework.web.struts.ActionSupport{
publicActionForwardexecute(ActionMappingmapping,ActionFormform,
HttpServletRequestrequest,HttpServletResponseresponse){
LoginFormloginForm=(LoginForm)form;//TODOAuto-generatedmethodstub
//获得WebApplicationContext对象
WebApplicationContextctx=this.getWebApplicationContext();
LoginDaodao=(LoginDao)ctx.getBean("loginDao");
Useru=newUser();
u.setName(loginForm.getName());
u.setPwd(loginForm.getPwd());
if(dao.checkLogin(u)){
returnmapping.findForward("success");
}else{
returnmapping.findForward("error");
}
}
}
applicationContext.xml中的配置
<beans>
<beanid=”loginDao”class=”com.cao.dao.LoginDao”/>
</beans>
这中配置方式同直接在web.xml文件配置差别不大。注意:Action继承自org.springframework.web.struts.ActionSupport使得struts和spring耦合在一起。
但实现了表示层和业务逻辑层的解耦(LoginDaodao=(LoginDao)ctx.getBean(“loginDao”))。
2,使用Spring的DelegatingRequestProcessor类
DelegatingRequestProcessor继承自org.apache.struts.action.RequestProcessor并覆盖了里面的方法。sturts-config.xml中<controllerprocessorClass=”org.springframework.web.struts.DelegatingRequestProcessor”/>通过<controller>来替代org.apache.struts.action.RequestProcessor的请求处理。
publicclassLoginActionextendsAction{
//利用spring来注入这个对象。
privateLoginDaodao;
publicvoidsetDao(LoginDaodao){
System.out.println("执行注入");
this.dao=dao;
}
publicLoginDaogetDao(){
returndao;
}
publicActionForwardexecute(ActionMappingmapping,ActionFormform,
HttpServletRequestrequest,HttpServletResponseresponse){
LoginFormloginForm=(LoginForm)form;//TODOAuto-generatedmethodstub
//这样一改这行代码似乎没有必要了。
//WebApplicationContextctx=this.getWebApplicationContext();
//LoginDaodao=(LoginDao)ctx.getBean("loginDao");
Useru=newUser();
u.setName(loginForm.getName());
u.setPwd(loginForm.getPwd());
//直接用dao来调用spring会将这个对象实例化。
if(dao.checkLogin(u)){
returnmapping.findForward("success");
}else{
returnmapping.findForward("error");
}
}
}
这里的LoginActionextendsAction说明struts每有和spring耦合。
看一下applicationContext.xml中的配置。
<beans>
<beanid=”loginDao”class=”com.cao.dao.LoginDao”/>
<beanname=”/login”class=”com.cao.struts.action.LoginAction”>
<propertyname=”dao”>
<reflocal=”loginDao”/>
</property>
</bean>
</beans>
这里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类的实现方式相同)。
1,<action>中type指向的是spring的代理类
<struts-config>
<data-sources/>
<form-beans>
<form-beanname=”loginForm”type=”com.cao.struts.form.LoginForm”/>
</form-beans>
<global-exceptions/>
<global-forwards/>
<action-mappings>
<!–type指向的是spring的代理类–>
<action
attribute=”loginForm”
input=”login.jsp”
name=”loginForm”
path=”/login”
scope=”request”
type=”org.springframework.web.struts.DelegatingActionProxy”>
<forwardname=”success”path=”/ok.jsp”/>
<forwardname=”error”path=”/error.jsp”/>
</action>
</action-mappings>
<message-resourcesparameter=”com.cao.struts.ApplicationResources”/>
<plug-inclassName=”org.springframework.web.struts.ContextLoaderPlugIn”>
<set-propertyproperty=”contextConfigLocation”value=”/WEB-INF/applicationContext.xml”/>
</plug-in>
</struts-config>
2,去掉struts-config.xml中<controller>
三种整和方式中我们优先选用全权委托的方式。
理由:
1,第一种使得过多的耦合了Spring和Action.
2,RequestProcessor类已经被代理如果要再实现自己的实现方式(如:编码处理)怕有点麻烦。
总结一下:
整合工作中的步骤:
1.修改struts-config.xml
2.配置applicationContext.xml
3.为Action添加get/set方法来获得依赖注入的功能。
10th05-2012分类:
1.使用Spring的ActionSupport。
2.使用Spring的DelegatingRequestProcessor类。
3.全权委托。
无论用那种方法来整合第一步就是要为struts来装载spring的应用环境。就是在struts中加入一个插件。struts-config.xml中
<plug-inclassName=”org.springframework.web.struts.ContextLoaderPlugIn”>
<set-propertyproperty=”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的代码:
publicclassLoginActionextendsorg.springframework.web.struts.ActionSupport{
publicActionForwardexecute(ActionMappingmapping,ActionFormform,
HttpServletRequestrequest,HttpServletResponseresponse){
LoginFormloginForm=(LoginForm)form;//TODOAuto-generatedmethodstub
//获得WebApplicationContext对象
WebApplicationContextctx=this.getWebApplicationContext();
LoginDaodao=(LoginDao)ctx.getBean("loginDao");
Useru=newUser();
u.setName(loginForm.getName());
u.setPwd(loginForm.getPwd());
if(dao.checkLogin(u)){
returnmapping.findForward("success");
}else{
returnmapping.findForward("error");
}
}
}
applicationContext.xml中的配置
<beans>
<beanid=”loginDao”class=”com.cao.dao.LoginDao”/>
</beans>
这中配置方式同直接在web.xml文件配置差别不大。注意:Action继承自org.springframework.web.struts.ActionSupport使得struts和spring耦合在一起。
但实现了表示层和业务逻辑层的解耦(LoginDaodao=(LoginDao)ctx.getBean(“loginDao”))。
2,使用Spring的DelegatingRequestProcessor类
DelegatingRequestProcessor继承自org.apache.struts.action.RequestProcessor并覆盖了里面的方法。sturts-config.xml中<controllerprocessorClass=”org.springframework.web.struts.DelegatingRequestProcessor”/>通过<controller>来替代org.apache.struts.action.RequestProcessor的请求处理。
publicclassLoginActionextendsAction{
//利用spring来注入这个对象。
privateLoginDaodao;
publicvoidsetDao(LoginDaodao){
System.out.println("执行注入");
this.dao=dao;
}
publicLoginDaogetDao(){
returndao;
}
publicActionForwardexecute(ActionMappingmapping,ActionFormform,
HttpServletRequestrequest,HttpServletResponseresponse){
LoginFormloginForm=(LoginForm)form;//TODOAuto-generatedmethodstub
//这样一改这行代码似乎没有必要了。
//WebApplicationContextctx=this.getWebApplicationContext();
//LoginDaodao=(LoginDao)ctx.getBean("loginDao");
Useru=newUser();
u.setName(loginForm.getName());
u.setPwd(loginForm.getPwd());
//直接用dao来调用spring会将这个对象实例化。
if(dao.checkLogin(u)){
returnmapping.findForward("success");
}else{
returnmapping.findForward("error");
}
}
}
这里的LoginActionextendsAction说明struts每有和spring耦合。
看一下applicationContext.xml中的配置。
<beans>
<beanid=”loginDao”class=”com.cao.dao.LoginDao”/>
<beanname=”/login”class=”com.cao.struts.action.LoginAction”>
<propertyname=”dao”>
<reflocal=”loginDao”/>
</property>
</bean>
</beans>
这里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类的实现方式相同)。
1,<action>中type指向的是spring的代理类
<struts-config>
<data-sources/>
<form-beans>
<form-beanname=”loginForm”type=”com.cao.struts.form.LoginForm”/>
</form-beans>
<global-exceptions/>
<global-forwards/>
<action-mappings>
<!–type指向的是spring的代理类–>
<action
attribute=”loginForm”
input=”login.jsp”
name=”loginForm”
path=”/login”
scope=”request”
type=”org.springframework.web.struts.DelegatingActionProxy”>
<forwardname=”success”path=”/ok.jsp”/>
<forwardname=”error”path=”/error.jsp”/>
</action>
</action-mappings>
<message-resourcesparameter=”com.cao.struts.ApplicationResources”/>
<plug-inclassName=”org.springframework.web.struts.ContextLoaderPlugIn”>
<set-propertyproperty=”contextConfigLocation”value=”/WEB-INF/applicationContext.xml”/>
</plug-in>
</struts-config>
2,去掉struts-config.xml中<controller>
三种整和方式中我们优先选用全权委托的方式。
理由:
1,第一种使得过多的耦合了Spring和Action.
2,RequestProcessor类已经被代理如果要再实现自己的实现方式(如:编码处理)怕有点麻烦。
总结一下:
整合工作中的步骤:
1.修改struts-config.xml
2.配置applicationContext.xml
3.为Action添加get/set方法来获得依赖注入的功能。
相关文章推荐
- 整合Spring与Struts1的三种方法总结
- Spring与Struts1整合的三种方法总结
- Spring整合Struts1.X的三种方法
- Spring整合Struts1.X的三种方法
- spring+hibernate+struts 整合的三种方法,我只用第三种可以。
- Spring整合Struts的三种方法
- Struts整合spring方法(三)
- Spring 和struts 整合的三种方式
- Struts 动作整合到 Spring 框架中的三种窍门
- 用 Spring 更好地处理 Struts 动作三种整合 Struts 应用程序与 Spring 的方式
- Struts与Spring整合的几种方法
- Spring与struts的整合方法(转)
- Spring 和struts 整合的三种方式
- 用 Spring 更好地处理 Struts 动作三种整合 Struts 应用程序与 Spring 的方式
- 整合Spring与Struts的几种方法
- Spring 和struts 整合的三种方式
- spring整合struts时创建ApplicationContext的方法
- spring整合struts时创建ApplicationContext的方法
- struts hibernate spring整合总结
- Spring与Struts的整合总结