Spring和Struct整合的三个方法
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中完成。
3,全权委托:
Action 的创建和对象的依赖注入全部由IOC容器来完成。使用Spring的DelegatingActionProxy来帮助实现代理的工作
org.springframework.web.struts.DelegatingActioProxy继承于org.apache.struts.action.Action.
全权委托的配置方式同 方式 2 类似 (applcationContext.xml文件的配置和 Action类的实现方式相同)。
1, <action>中 type指向的是spring 的代理类
<struts-config>
<data-sources />
<form-beans >
<form-bean name=”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” >
<forward name=”success” path=”/ok.jsp” />
<forward name=”error” path=”/error.jsp” />
</action>
</action-mappings>
<message-resources parameter=”com.cao.struts.ApplicationResources” />
<plug-in className=”org.springframework.web.struts.ContextLoaderPlugIn”>
<set-property property=”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和Struct整合的三个方法的更多相关文章
- Spring框架中整合JUnit单元测试的方法
一. 步骤: 1. 拷贝jar包: 1. JUnit-4.9.jar和spring-test-4.2.4.RELEASE.jar ; 2. 替换原来的main函数: 1. 在测试类上使用注解方式替换: ...
- Spring Boot 注册 Servlet 的三种方法,真是太有用了!
本文栈长教你如何在 Spring Boot 注册 Servlet.Filter.Listener. 你所需具备的基础 什么是 Spring Boot? Spring Boot 核心配置文件详解 Spr ...
- 009-shiro与spring web项目整合【三】验证码、记住我
一.验证码 1.自定义FormAuthenticationFilter 需要在验证账号和名称之前校验验证码 /** * * <p>Title: CustomFormAuthenticati ...
- Spring、实例化Bean的三种方法
1.使用类构造器进行实例化 <bean id="personIService" class="cn.server.impl.PersonServiceImpl&qu ...
- spring集成JPA的三种方法配置
JPA是Java EE5规范之一,是一个orm规范,由厂商来实现该规范.目前有hibernate,OpenJPA,TopLink和EclipseJPA等实现 spring提供三种方法集成JPA:1.L ...
- MyBatis 与 Spring 的完美整合方法
MyBaits 整合 Spring MyBatis-Spring 项目 第一步:创建测试工程 第二步:引入依赖 jar 包 第三步:编写 Spring 配置文件 第四步:编写 MyBatis 配置文件 ...
- Spring使用jdbcJdbcTemplate和三种方法配置数据源
三种方法配置数据源 1.需要引入jar包:spring-jdbc-4.3.2.RELEASE.jar <!-- spring内置,springJdbc,配置数据源 --> <bean ...
- spring注入bean的三种方法
在Spring的世界中, 我们通常会利用bean config file 或者 annotation注解方式来配置bean. 在第一种利用bean config file(spring xml)方式中 ...
- Spring实例化Bean三种方法:构造器、静态工厂、实例工厂
Spring中Bean相当于java中的类,可以通过xml文件对bean进行配置和管理. 一.Bean的实例化: 构造器实例化.静态工厂实例化.实例工厂方式实例化. 目录: 构造器实例化: xml配置 ...
随机推荐
- iOS开发UI篇——Button基础
一.简单说明 一般情况下,点击某个控件后,会做出相应反应的都是按钮 按钮的功能比较多,既能显示文字,又能显示图片,还能随时调整内部图片和文字的位置 二.按钮的三种状态 1. normal(普通状态) ...
- JS特殊符号
反斜杠用来在文本字符串中插入省略号.换行符.引号和其他特殊字符. 代码 输出 \' 单引号 \" 双引号 \& 和号 \\ 反斜杠 \n 换行符 \r 回车符 \t 制表符 \b 退 ...
- Servlet(三)
重定向 服务器向浏览器发送一个302状态码以及一个Location消息头(该消息头的值是一个地址,称之为重定向地址),浏览器收到后会立即向重定向的地址发出请求,使用相应对象的API方法实现(respo ...
- Oracle IN 传递字符串参数查询失效
在写存储过程中有如下代码: FOR a IN ( SELECT a.svo_no,a.AUDIT_NO,a.order_id FROM TT_PI_MODEL_REL a ) LOOP SELECT ...
- form WebBrowser自动点击弹出提示框alert、弹出对话框confirm、屏蔽弹出框、屏蔽弹出脚本错误的解决办法
针对WebBrowser控件中自动点击弹出框及禁用脚本提示问题得到如下几种实际情况的解决办法,绝对管用. 1.屏蔽弹出错误脚本 将WebBrowser控件ScriptErrorsSuppressed设 ...
- MySql数据库3【优化3】缓存设置的优化
1.表缓存 相关参数: table_open_cache 指定表缓存的大小.每当MySQL访问一个表时,如果在表缓冲区中还有空间,该表就被打开并放入其中,这样可以更快地访问表内容.通过检查峰值时间的状 ...
- python连接mysql之pymysql模块
以下demo均以python2中的mysqldb模块 一.插入数据 ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 import MySQLdb conn = MyS ...
- K - 计算球体积
Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u Description 根据输入的 ...
- tomcat架构分析-索引
出处:http://gearever.iteye.com tomcat架构分析 (概览) tomcat架构分析 (容器类) tomcat架构分析 (valve机制) tomcat架构分析 (valve ...
- MVC架构模式
MVC是一个框架模式,它强制性的使应用程序的输入.处理和输出分开.使用MVC应用程序被分成三个核心部件:模型.视图.控制器. V页面传递数据给C,C调用模型处理返回数据给V 最典型的MVC就是JSP ...