整合Spring与Struts1的三种方法总结

无论用那种方法来整合,第一步都是要装载spring的应用环境,有三种方式:

#1. struts-config.xml

<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.2//EN"
  "http://struts.apache.org/dtds/struts-config_1_2.dtd">

<struts-config>

<plug-in className="org.springframework.web.struts.ContextLoaderPlugIn">

<set-property property="contextConfigLocation" value="/WEB-INF/applicationContext.xml"/>

</plug-in>

</struts-config>

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

#2. web.xml

<web-app xmlns="http://java.sun.com/xml/ns/javaee"

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"

version="3.0">

<context-param>

<param-name>contextConfigLocation</param-name>

<param-value>/WEB-INF/applicationContext.xml</param-value>

</context-param>

<listener>

<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>

</listener>

</web-app>

#3. web.xml(低版本tomcat)

web.xml

<web-app xmlns="http://java.sun.com/xml/ns/javaee"

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"

version="3.0">

<servlet>

<servlet-name>SpringContextServlet</servlet-name>

<servlet-class>org.springframework.web.context.ContextLoaderServlet</servlet-class>

</servlet>

</web-app>

1.使用Spring 的 ActionSupport 。

Struts的Action继承Spring的ActionSupport类,这样Struts 1.x就融入Spring框架中了。

Spring 的ActionSupport 继承至 org.apache.struts.action.Action, ActionSupport的子类可以获得 WebApplicationContext类型的全局变量。通过getWebApplicationContext()可以获得这个变量。

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");

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

<struts-config>

<controller processorClass="org.springframework.web.struts.DelegatingRequestProcessor"/>

</struts-config>

applicationContext.xml

<beans xmlns="http://www.springframework.org/schema/beans"

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xsi:schemaLocation="http://www.springframework.org/schema/beans

http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">

<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的DelegatingAcionProxy来帮助实现代理的工作.
org.springframework.web.struts.DelegatingActiongProxy继承于org.apache.struts.action.Action.全权委托的配置方式同 方式 2 类似 (applcationContext.xml文件的配置和 Action类的实现方式相同)。

1) <action>中 type指向的是spring 的代理类

sturts-config.xml

<struts-config>

<form-beans >

<form-bean name="loginForm" type="com.cao.struts.form.LoginForm" />

</form-beans>

<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>

<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方法 来获得依赖注入的功能。

Struts2与Spring的整合

•Struts2框架为配合与Spring3框架进行整合,提供了相应的拦截器。

•该组件名为StrutsSpringObjectFactory,位于struts2-spring-plugin-2.2.1.1.jar中

•通过在struts.xml中的声明,便可直接使用该组件以实现整合。

struts.xml

6  <struts>

7      <constant name="struts.objectFactory" value="org.apache.struts2.spring.StrutsSpringObjectFactory"/>

8      <package name="strutsspring" namespace="/strutsspring" extends="struts-default">

9          <action name="login" class="LoginAction">

10             <result name="success">/success.jsp</result>

11             <result name="input">/index.jsp</result>

12         </action>

13     </package>

14 </struts>

ApplicationContext.xml

1  <?xml version="1.0" encoding="UTF-8"?>

2  <beans xmlns="http://www.springframework.org/schema/beans"

3         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

4         xmlns:context="http://www.springframework.org/schema/context"

5         xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd

6         http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd">

7

8      <bean id="LoginService" class="model.LoginService">

9      </bean>

10

11     <bean id="LoginAction" class="controller.LoginAction" scope="prototype">

12         <property name="loginService" ref="LoginService"/>

13     </bean>

14 </beans>

Spring的事务模型

CMT事务属性

企业Bean 的默认事务边界管理设置是CMT。

1 @Stateless

2 @TransactionManagement(CONTAINER)

3 public PayrollBean implements Payroll {

4     //..

5 }

@TransactionAttribute(TransactionAttributeType.MANDATORY)

public void setBenefitsDeduction(int empId, double deduction) {...}

Java EE (14) -- SSH配置的更多相关文章

  1. JAVA EE 运行环境配置(包含JAVA SE)

    JAVA EE 运行环境配置(包含JAVA SE) 1.下载并安装jre-7u7-windows-i586.exe (最新的JAVA运行环境) 2.下载并安装java_ee_sdk-6u4-jdk7- ...

  2. 【转】eclipse for java ee的tomcat配置(常见问题解决)

    原文:http://blog.csdn.net/lanzhizhuxia/article/details/8087709 前一段时间准备学习ssh的源码,但是web开发的环境一直没有弄好,myecli ...

  3. linux jdk,java ee ,tomcat 安装配置

    1.把mypagekage.iso 挂载到linux操作系统中. 在VM做好配置,使用 mount /mnt/cdrom 2.把安装文件拷贝到/home cp 文件名 /home (快捷键tab) 3 ...

  4. 最重要的 Java EE 最佳实践

    參考:IBM WebSphere 开发人员技术期刊: 最重要的 Java EE 最佳实践 IBM WebSphere 开发人员技术期刊: 最重要的 Java EE 最佳实践 2004 年 IBM® W ...

  5. java ee eclipse 配置 ssh框架

    mvnDebug tomcat:run 这条命令主要用来远程测试,它会监听远程测试用的8000端口,在eclipse里打开远程测试后,它就会跑起来了,设断点,调试,一切都是这么简单. 0.如果是mav ...

  6. 【Java EE 学习 69 中】【数据采集系统第一天】【SSH框架搭建】

    经过23天的艰苦斗争,终于搞定了数据采集系统~徐培成老师很厉害啊,明明只是用了10天就搞定的项目我却做了23天,还是模仿的...呵呵,算了,总之最后总算是完成了,现在该好好整理该项目了. 第一天的内容 ...

  7. 【Java EE 学习 67 下】【OA项目练习】【SSH整合JBPM工作流】【JBPM项目实战】

    一.SSH整合JBPM JBPM基础见http://www.cnblogs.com/kuangdaoyizhimei/p/4981551.html 现在将要实现SSH和JBPM的整合. 1.添加jar ...

  8. JavaWeb学习----JSP简介及入门(含Eclipse for Java EE及Tomcat的配置)

    ​[声明] 欢迎转载,但请保留文章原始出处→_→ 生命壹号:http://www.cnblogs.com/smyhvae/ 文章来源:http://www.cnblogs.com/smyhvae/p/ ...

  9. 用一天的时间学习Java EE中的SSH框架

    首先说明一下,本人目前主要从事.NET领域的工作,但对于C++.Java.OC等语言也略知一二,周末闲来无事,特花费一天的时间学习了一下Java中的SSH框架,希望把学习过程中的心得体会与园友们进行分 ...

随机推荐

  1. linux消息队列的使用

    消息队列 *消息队列是内核地址空间中的内部链表,通过内核在各个进程之间传递的内容.消息顺序发送到消息队列中,每个消息队列都有IPC标识符唯一地进行标识. msgbuf结构 struct msgbuf{ ...

  2. 机器学习中的数学-线性判别分析(LDA), 主成分分析(PCA)

    转:http://www.cnblogs.com/LeftNotEasy/archive/2011/01/08/lda-and-pca-machine-learning.html 版权声明: 本文由L ...

  3. asp.net asp:TextBox控件绑定值后,获取不到新值问题解决方法

    把Page_Load里绑定的代码放在    if(!IsPostBack){}里面后,即可获取到更新的值. 意思为第一次加载执行.

  4. 为什么数据可以从pl/sql查出来而使用ado.net查询,结果却是空?

    1.背景 一条记录(如select * from A where a='1'),使用pl/sql作为条件可以查询出记录,但用ado.net sql查询结果却是空. 2.原因 a字段的数据类型的char ...

  5. VS2010 创建WindowsService服务

    1.新建一个Windows 服务 2.添加Installer 这一步很重要,在处理完你的业务逻辑后需要添加一个Installer才能是你的Windows服务被安装. 在VS中添加Installer 右 ...

  6. Appium绑定

    锁定 锁定屏幕 # python driver.lock(5) 将 app 置于后台 把当前应用放到后台去 # python driver.background_app(5) 收起键盘 收起键盘 # ...

  7. mahout安装配置

    1.下载mahout 下载地址:http://mahout.apache.org 我下载的最新版:mahout-distribution-0.9 2.把mahout解压到你想存放的文档,我是放在/Us ...

  8. Android service的开启和绑定,以及调用service的方法

    界面: <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android= ...

  9. lintcode:递归打印数字

    题目 用递归打印数字 用递归的方法找到从1到最大的N位整数. 样例 给出 N = 1, 返回[1,2,3,4,5,6,7,8,9]. 给出 N = 2, 返回[1,2,3,4,5,6,7,8,9,10 ...

  10. 【memcache缓存专题(3)】PHP-memcache扩展的安装以及使用

    安装PHP-memcache扩展和安装其他PHP扩展的步骤是一样的. 安装 step 1:搜索下载扩展 http://pecl.php.net/package/memcache step 2: gzi ...