Spring整合Strut2小结

来源:互联网 发布:淘宝店铺的定位 编辑:程序博客网 时间:2024/06/05 05:53

自己动手试下整合spring和Strut2的最简单的一个小实验,使用的版本是struts-2.3.24spring-framework-4.1.4.RELEASE-dist

                整合参考的struts2文档介绍url:http://struts.apache.org/docs/spring-plugin.html

                由于没有经验出现了问题和收获,小结如下:

                1.只搭struts2能正常运行,一加上spring就出问题了,程序运行不起来,tomcat报了各自错误

                 原因是需要的jar包没有全部导入,之前学习看的视频是spring2.5.6的,它的spring.jar包里面包含了各自需要的jar包集合,而spring4.1.4是分开在libs目录下,最后可以正常整合需要的最小包如下图(正常运行不包括spring的其他事务通知,aop控制),总结一句:不同版本的整合可能需要不同的jar包,为了减少折腾应该从框架相应文档目录或者原来项目或者平时整合资料中寻找现成需要的jar包.例如当时struts2的包就是从struts-2.3.24-all\struts-2.3.24\apps\struts2-blank的lib copy出来用的,运行时发现缺少commons-loging,添加上后正常了


2.搭struts2框架时候经常出现找不到mapping action或者404错误

                解决思路,首先看路径是否写对,然后再看tomac时候正常启动该web app,如果发生了异常,从异常着手。

                

3 小结spring整合struts2的粗浅认识机制

                spring负责了strut2 action对象的创建,当stucts的拦截器收到action请求时,首先strut2会去查看struts.xml 该action请求对应的class属性是否在spring容器里有对应的id属性,如果有则用该对象去处理action请求,不会再创建新的对象。如果没有在spring容器中找到对应的id,spring则会自己创建该对象,并完成的系列的依赖注入。

2个做实验的类 FirstAction 和 Apple,并把容器中的Apple类实例注入到FirstAction实例对象中中:

package spring.scope;import javax.annotation.Resource;public class FirstAction {private Apple apple;private String id;public FirstAction(){System.out.println("FirstAction 被创建了");}public String fun(){System.out.print("apple is " + apple);return "success";}public Apple getApple() {return apple;}@Resource(name="apple")public void setApple(Apple apple) {this.apple = apple;}public String getId() {return id;}public void setId(String id) {this.id = id;}}

package spring.scope;import org.springframework.stereotype.Component;public class Apple {public Apple(){System.out.println("Apple类被创建了。。");}}
struts.xml 和 spring.xml

注意:strut2.xml的firstaction的class属性和sprig.xml 的id属性

<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE struts PUBLIC"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN""http://struts.apache.org/dtds/struts-2.3.dtd"><struts> <span style="white-space:pre"></span><constant name="struts.objectFactory" value="spring" /><span style="white-space:pre"></span><package name="test" extends="struts-default"><span style="white-space:pre"></span><action name="firstaction" class=<span style="color:#ff0000;">"firstaction</span>" method="fun"><span style="white-space:pre"></span><result name="success">/success.html</result><span style="white-space:pre"></span></action><span style="white-space:pre"></span></package></struts>
</pre><pre name="code" class="html">注意:为了现实效果把id为apple的bean scope属性设置为prototype

<?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:context="http://www.springframework.org/schema/context"    xsi:schemaLocation="http://www.springframework.org/schema/beans        http://www.springframework.org/schema/beans/spring-beans.xsd        http://www.springframework.org/schema/aop        http://www.springframework.org/schema/aop/spring-aop.xsd        http://www.springframework.org/schema/context        http://www.springframework.org/schema/context/spring-context.xsd">                  <context:annotation-config/>        <context:component-scan base-package="spring"/>                    <bean id="apple" class="spring.scope.Apple" scope="<strong><span style="color:#ff0000;">prototype</span></strong>"/>          <bean id="<strong><span style="color:#ff0000;">firstaction</span></strong>" class="spring.scope.FirstAction">         <property name="id" value="2013"/>        </bean></beans>
运行结果如下:

当tomcat启动的时候并加载该web项目,这2个对象被spring 容器 创建!!随后我尝试多次请求改action,只输出fun方法的内容,没有创建新的firstaction(Action是多例的)从图中还能看出apple对象是同一个对象,证明了这句话”首先strut2会去查看struts.xml 该action请求对应的class属性是否在spring容器里有对应的id属性,如果有则用该对象去处理action请求,不会再创建新的对象“,如图

             

  如果把  struts.xml文件的 action name="firstaction" class="firstaction" method="fun"  改成   action name="firstaction" class="spring.scope.FirstAction" method="fun",

运行结果如下,当tomcat启动的时候并加载该web项目,这2个对象被spring 容器 创建!!随后我尝试多次请求该action,发现每次请求都产生了新的Apple对象和FirstAction对象,并且可以看到Apple每次都不是同一个对象,判断出这个Action对象是由spring创建并完成了依赖注入的并非struts创建,证明了这句话”。如果没有在spring容器中找到对应的id,spring则会自己创建改对象,并完成的系列的依赖注入“

              

</pre><pre name="code" class="html">

0 0
原创粉丝点击