码迷,mamicode.com
首页 > 其他好文 > 详细

SSH整合 第五篇 struts2的到来

时间:2016-08-22 23:10:39      阅读:161      评论:0      收藏:0      [点我收藏+]

标签:

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基本整合成功。目前导入了以下包。

技术分享

技术分享

 

SSH整合 第五篇 struts2的到来

标签:

原文地址:http://www.cnblogs.com/jway1101/p/5797081.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!