SSH整合 第五篇 struts2的到来

Struts2与Spring整合
本文介绍Struts2与Spring框架的整合过程,包括所需Jar包的配置、使用注解方式定义Action,以及如何将Action托管给Spring进行管理。通过实际操作验证整合的成功。

struts2的好处,web层的显示,同时Action类相当于MVC模式的C。整合进来的话,是通过与Spring整合,减少重复代码,利用IoC和AOP。

 

1、struts-2.5.2.jar

以上是struts-2.5.2基本的包 ,另外着色的两个之前有加入了,不再加入。

2、整合包

struts2-spring-plugin-2.5.2(struts2有提供)

 

3、Action要用注解的话

加入struts2-convention-plugin-2.5.2

 

在Action上加上注解时

有NoClassDefFoundError: org/objectweb/asm/ClassVisitor报错

加入asm-xxx.jar

有NoClassDefFoundError: org/objectweb/asm/commons/EmptyVisitor包

加入asm-commons-xxx.jar

加入这两个包就没有报错了。不过还有一个asm-tree-xxx.jar同样是字节码的处理jar。先将它加入吧。

 

4、初步了解struts2

参考 struts2入门程序

 

5、将Action托管给Spring

需要在struts.xml中加上常量。

<constant name="struts.objectFactory" value="spring"></constant> 

 

加上这个常量,且有一个包的struts配置文件如下。

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.5//EN"
    "http://struts.apache.org/dtds/struts-2.5.dtd">
    
<struts>
<!-- 开发模式(修改struts.xml不用重启服务器) -->
<constant name="struts.devMode" value="true" />

<!-- 将action托管给spring -->  
<constant name="struts.objectFactory" value="spring"></constant>  
     
<!-- 动态action  2.5-->
<constant name="struts.enable.DynamicMethodInvocation" value="true" />

<package name="myPackage" namespace="/user" extends="struts-default" >
    <!-- 动态action  2.5-->
    <global-allowed-methods>regex:.*</global-allowed-methods>
    
    <action name="userAction" class="com.xzw.ssh.action.UserAction">
        <result name="success">/index.jsp</result>
    </action>
</package> 

</struts>
struts.xml

 

6、整合成功。

可以尝试一下在Action中的注入一个属性,然后访问这个Action,看下有没有成功注入。

 

至此,三大框架Struts-2.5.2、Spring-3.2.0和Hibernate-4.2.21基本整合成功。目前导入了以下包。

 

转载于:https://www.cnblogs.com/jway1101/p/5797081.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值