spring+struts四种整合

第一种方式getter和setter
1、在struts中添加
<!--代理类-->
<controller processorClass="org.springframework.web.struts.DelegatingRequestProcessor" />


  <message-resources parameter="com.liuchangjiong.struts.ApplicationResources" />


  <!-- 必须放在message的下面 添加插件-->
  <plug-in className="org.springframework.web.struts.ContextLoaderPlugIn">
 <set-property property="contextConfigLocation"
  value="/WEB-INF/classes/applicationContext.xml" />
</plug-in>


2、applicationContext.xml中
 <bean name="/register" class="com.liuchangjiong.struts.action.RegisterAction">
  <property name="message">
   <value>Spring Struts</value>
  </property>
 </bean>
注意代码中的粗斜体部分,这个name 的取值一定要和Struts 配置文件action 中的path 的
值相对应,否则整合就会失败:
<action path="/userLogin" ......
。既然现在这个Action 类处于Spring 的配置和管理之下,自然就可以注入任意的属性,包
括message,以及以后可以加入的Service 层对象了,也就是说你现在可以完全把它看作
一个普通的JavaBean 来处理就行了,想加入AOP 什么的都可以。在这里为了测试方便,
我们把message 属性注入一个”Spring Struts”的值。


3、不需要在web.xml中注入

 <context-param>
  <param-name>contextConfigLocation</param-name>
  <param-value>/WEB-INF/classes/applicationContext.xml</param-value>
 </context-param>
 <listener>
  <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
 </listener>
 4、在Action中添加getter和setter

 private String message;
 public String getMessage() {
 return message;
 }
 public void setMessage(String message) {
 this.message = message;
 }
并且使用访问
System.out.println(getMessage());

第二种方式:
方式二:使用ContextLoaderPlugIn,替换Struts Action 的类型并在Spring 中配置对
应的类型。

1、这种方式下的Action类的源码和上面11.7.1 给Action类加入message属性一节所介绍
的是一样的
2、Spring的配置文件也和11.7.3 在Spring配置文件中加入Action的bean定义一
节所介绍的内容一样。
3、不需要在web.xml中注入
4、唯一不同的是Struts 的配置文件, 注意看下面的配置文件
struts-config.xml源码:
****
去掉
<controller
processorClass="org.springframework.web.struts.DelegatingRequestProcessor"
/>

但是要将action中的type="org.springframework.web.struts.DelegatingActionProxy"


经测试成功

第一个不同点,就是Action 类的type 从原来的
com.yourcompany.struts.action.UserLoginAction 变成了现在的由Spring 所提供的代理类
org.springframework.web.struts.DelegatingActionProxy,这个类也是Struts Action 类的一
个子类,只不过它的幕后工作就是从Spring 配置文件中查找定义的name 为/userLogin 的
bean 定义来作为真正的Action 对象,然后委托给它来处理剩下的业务逻辑。
第二个不同点,
就是删除了下面这句配置语句:<controller processorClass="
org.springframework.web.struts.DelegatingRequestProcessor" />。那么这种方式总体上来
说代码改动量也不太大,实际开发中可以采用。

 

第三种方法(开发中不提倡)
使用Spring 提供的ActionSupport 类。这种方式,因为Action 类的代码要做
大量修改,已经违背了Spring 提倡的无侵入这个理念,而且的确出现代码改动量太多的这
个问题,因此在实际开发中不提倡使用这种方式。
3、不需要在web.xml中注入
4、struts.xml中发生变化
恢复type为type="com.yourcompany.struts.action.UserLoginActionSupport"

而且插件为
<plug-in className="org.springframework.web.struts.ContextLoaderPlugIn">
<set-property property="contextConfigLocation"
value="/WEB-INF/classes/applicationContext.xml" />
</plug-in>


相比较起没加入 Spring 之前的Struts 的配置文件,其不同之处就如代码中粗斜体部分
所示,加入了一个插件配置。那么在这种方式下,不需要在Spring 的配置文件中加入Action
类的bean 定义,相对比的,您需要首先继承自ActionSupport 类,然后通过在Action 类中
写代码来自己查找Bean 类:
--------------上面的说法是不需要在Spring 的配置文件中加入Action类的bean 定义


/** 基于 Spring ActionSupport 的登录类。*/
public class UserLoginActionSupport extends ActionSupport {
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) {
//*********
ApplicationContext ctx = this.getWebApplicationContext();
StudentManager studentManager = (StudentManager) ctx
.getBean("studentManager");
System.out.println("登录检查="
+ studentManager.checkLogin("spring hibernate 标注事务测试", "密码
"));
//*********
// 更多业务代码
return mapping.findForward("success");
}
--------------继承ActionSupport 类

代码中的粗斜体部分就是新增的内容,主要就是获得Spring 的核心容器类:
ApplicationContext(可以认为和BeanFactory 这个类工厂是一回事),然后从里面获取定义
好的bean 类,之后调用上面的业务逻辑。其实这种方式和自己亲手写代码创建BeanFactoy
然后加载对象是差不多的,对现有代码的改动也比较大,既有继承,还有额外代码,还得导
入Spring 的包,Spring 文档上说推荐这种做法,我怎么看也看不出这样到底有多少方便的
地方。
提示:如果您需要其它类型的Action,例如DispatchAction 等等,那么需要继承自对
应的Spring 版本的类―― ActionSupport,DispatchActionSupport,
LookupDispatchActionSupport,MappingDispatchActionSupport,只不过是类名后加
了一个Support,这些类位于包org.springframework.web.struts 下面。

第四种

最后, 我们再介绍 Struts1 加载Spring 的另一种方式,修改web.xml 配置加载spring
上下文环境,其配置方式如下:
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext.xml</param-value>
</context-param>
通过listener 加载:
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener
-class>
</listener>
或者利用severlet 类在启动时加载:
<servlet>
<servlet-name>context</servlet-name>
<servlet-class>org.springframework.web.context.ContextLoaderServlet</servlet-c
lass>
<load-on-startup>1</load-on-startup>
</servlet>
注意:使用这种方式的时候,就不要在 Strurts 的配置文件中写那个 plugin 配置信息
了。

 

建立类StudentManager 和 它的方法checkLogin
package com.liuchangjiong.struts.action;

public class StudentManager {

 public String checkLogin(String string, String string2) {
  
  return string+string2;
 }

}

注意在applicationContext.xml中声明,注意是id,与上面都是name
 <bean id="studentManager" class="com.liuchangjiong.struts.action.StudentManager">
  
 </bean>
而且该类不用在struts.xml中声明

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值