spring+struts的集成(第二种集成方案,推荐)
来源:互联网 发布:李米的猜想知乎 编辑:程序博客网 时间:2024/06/10 13:55
spring+struts的集成(第二种集成方案)
原理:将业务逻辑对象通过spring注入到Action中,从而避免了在Action类中的直接代码查询
1、spring和struts依赖库配置
* 配置struts
--拷贝struts类库和jstl类库
--修改web.xml文件来配置ActionServlet
--提供struts-config.xml文件
--提供国际化资源文件
* 配置spring
--拷贝spring类库
--提供spring配置文件
2、因为Action需要调用业务逻辑方法,所以需要在Action中提供setter方法,让spring将业务逻辑对象注入过来
3、在struts-config.xml文件中配置Action
* <action>标签中的type属性需要修改为org.springframework.web.struts.DelegatingActionProxy
DelegatingActionProxy是一个Action,主要作用是取得BeanFactory,然后根据<action>中的path属性值
到IoC容器中取得本次请求对应的Action
struts-config.xml配置文件
4、在spring配置文件中需要定义struts的Action,如:
<bean name="/login" class="com.bjsxt.usermgr.actions.LoginAction" scope="prototype">
<property name="userManager" ref="userManager"/>
</bean>
* 必须使用name属性,name属性值必须和struts-config.xml文件中<action>标签的path属性值一致
* 必须注入业务逻辑对象
* 建议将scope设置为prototype,这样就避免了struts Action的线程安全问题
spring配置文件:
原理:将业务逻辑对象通过spring注入到Action中,从而避免了在Action类中的直接代码查询
1、spring和struts依赖库配置
* 配置struts
--拷贝struts类库和jstl类库
--修改web.xml文件来配置ActionServlet
--提供struts-config.xml文件
--提供国际化资源文件
* 配置spring
--拷贝spring类库
--提供spring配置文件
2、因为Action需要调用业务逻辑方法,所以需要在Action中提供setter方法,让spring将业务逻辑对象注入过来
public class LoginAction extends Action {private UserManager userManager;@Overridepublic ActionForward execute(ActionMapping mapping, ActionForm form,HttpServletRequest request, HttpServletResponse response)throws Exception {LoginActionForm laf = (LoginActionForm)form;userManager.login(laf.getUsername(), laf.getPassword());return mapping.findForward("success");}public void setUserManager(UserManager userManager) {this.userManager = userManager;}}
3、在struts-config.xml文件中配置Action
* <action>标签中的type属性需要修改为org.springframework.web.struts.DelegatingActionProxy
DelegatingActionProxy是一个Action,主要作用是取得BeanFactory,然后根据<action>中的path属性值
到IoC容器中取得本次请求对应的Action
struts-config.xml配置文件
<struts-config><form-beans><form-bean name="loginForm" type="com.bjsxt.usermgr.forms.LoginActionForm"/></form-beans><action-mappings><action path="/logininput"forward="/login.jsp"></action><action path="/login"type="org.springframework.web.struts.DelegatingActionProxy"name="loginForm"scope="request"><forward name="success" path="/success.jsp"/></action></action-mappings> <message-resources parameter="MessageResources" /></struts-config><plug-in className="org.springframework.web.struts.ContextLoaderPlugIn"> <set-property property="contextConfigLocation" value="/WEB-INF/applicationContext.xml,/WEB-INF/applicationContext-*.xml" /></plug-in>
4、在spring配置文件中需要定义struts的Action,如:
<bean name="/login" class="com.bjsxt.usermgr.actions.LoginAction" scope="prototype">
<property name="userManager" ref="userManager"/>
</bean>
* 必须使用name属性,name属性值必须和struts-config.xml文件中<action>标签的path属性值一致
* 必须注入业务逻辑对象
* 建议将scope设置为prototype,这样就避免了struts Action的线程安全问题
spring配置文件:
<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd"><bean name="/login" class="com.bjsxt.usermgr.actions.LoginAction" scope="prototype"><property name="userManager" ref="userManager"/></bean> <bean id="userManager" class="com.bjsxt.usermgr.manager.UserManagerImpl"/></beans>
0 0
- spring+struts的集成(第二种集成方案,推荐)
- Spring+Struts集成(第二种方案)
- spring+struts的集成(第二种集成方案,用得最多的方案)
- 探讨Spring与Struts 的集成方案
- spring+struts集成(两种方案)
- spring+struts的集成(第一种集成方案)
- spring+struts的集成(第一种集成方案)
- spring+struts的集成(第一种集成方案)
- Spring+struts+hibernate 集成方案
- struts与spring集成方案
- 深入探讨Spring与Struts的集成方案深入探讨Spring与Struts的集成方案
- spring+struts的集成
- spring+struts的集成
- 深入探讨Spring与Struts的集成方案
- 深入探讨 Spring 与 Struts 的集成方案
- 深入探讨 Spring 与 Struts 的集成方案(1)
- 深入探讨 Spring 与 Struts 的集成方案(2)
- 深入探讨 Spring 与 Struts 的集成方案(3)
- KKT条件
- spring对AOP的支持(采用配置文件的方式)
- spring对AOP的支持(JDK的动态代理实现AOP和CGLIB实现AOP)
- spring,hibernate集成
- spring+struts的集成(第一种集成方案)
- spring+struts的集成(第二种集成方案,推荐)
- spring的2种加载方式:ContextLoaderListener和ContextLoaderPlugIn
- Java 文件上传,使用FileItem解析时,出现乱码的解决办法
- ListView分页加载,数据由NOHttp加载
- 在Struts1.x中使用net.sf.struts.saif.SAIFSpringPlugin配置Interceptor(拦截器)
- junit单元测试的java.lang.NoClassDefFoundError: org/hamcrest/SelfDescribing问题解决办法
- Java堆内存Heap与非堆内存Non-Heap简介和设置
- 如何安装配置你的Tomcat5并绑定域名
- 不同机型中使用j2me调出拨号和发短消息