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

 

1、struts-2.5.2.jar

SSH整合 第五篇 struts2的到来

以上是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

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-04-23
  • 2021-10-17
  • 2021-04-08
  • 2022-01-26
  • 2021-12-02
猜你喜欢
  • 2021-08-13
  • 2021-05-25
  • 2022-12-23
  • 2021-04-22
  • 2021-05-15
  • 2022-12-23
  • 2021-10-02
相关资源
相似解决方案