struts2+Hibernate4+spring3+EasyUI环境搭建之三:引入sututs2以及spring与sututs2整合

1、引入struts2

<!-- struts2 和心包 排除javassist  因为hibernate也有 会发生冲突-->
        <dependency>
            <groupId>org.apache.struts</groupId>
            <artifactId>struts2-core</artifactId>
            <version>2.3.4.1</version>
 
</dependency>
        <!-- struts2和spring整合包 这样会使action对每个请求new一个action对象 非单例
        注意区别:springMVC注解是Cotroller 是单例的-->
        <dependency>
            <groupId>org.apache.struts</groupId>
            <artifactId>struts2-spring-plugin</artifactId>
            <version>2.3.4.1</version>
        </dependency>
        <!-- struts2注解支持包-->
        <dependency>
            <groupId>org.apache.struts</groupId>
            <artifactId>struts2-convention-plugin</artifactId>
            <version>2.3.4.1</version>
        </dependency>

2、引入struts配置文件

<?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>

    <!-- 指定由spring负责action对象的创建 -->
    <constant name="struts.objectFactory" value="spring" />
    <!-- 所有匹配*.action的请求都由struts2处理 可以value='action,do'-->
    <constant name="struts.action.extension" value="action" />
    <!-- 是否启用开发模式 生产环境false-->
    <constant name="struts.devMode" value="true" />
    <!-- struts配置文件改动后,是否重新加载  生产环境false-->
    <constant name="struts.configuration.xml.reload" value="true" />
    <!-- 设置浏览器是否缓存静态内容 生产环境true-->
    <constant name="struts.serve.static.browserCache" value="false" />
    <!-- 请求参数的编码方式 -->
    <constant name="struts.i18n.encoding" value="utf-8" />
    <!-- 每次HTTP请求系统都重新加载资源文件,有助于开发 生产环境false -->
    <constant name="struts.i18n.reload" value="true" />
    <!-- 文件上传最大值 单位:Byte-->
    <constant name="struts.multipart.maxSize" value="104857600" />
    <!-- 让struts2支持动态方法调用 -->
    <constant name="struts.enable.DynamicMethodInvocation" value="true" />
    <!-- Action名称中是否还是用斜线 -->
    <constant name="struts.enable.SlashesInActionNames" value="false" />
    <!-- 允许标签中使用表达式语法 注意OGNL可能速度上比jstl慢一些 -->
    <constant name="struts.tag.altSyntax" value="true" />
    <!-- 对于WebLogic,Orion,OC4J此属性应该设置成true -->
    <constant name="struts.dispatcher.parametersWorkaround" value="false" />

    <package name="basePackage" extends="struts-default">
    </package>

</struts>

3、在web.xml加入sututs2的配置处理

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0" 
    xmlns="http://java.sun.com/xml/ns/javaee" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
    http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
  <display-name></display-name>
  <!-- spring配置文件位置 -->    
  <context-param>
      <param-name>contextConfigLocation</param-name>
      <param-value>classpath:spring.xml</param-value>
  </context-param>
  <!-- Struts2配置 -->
    <filter>
        <filter-name>struts2</filter-name>
        <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
    </filter>
    <filter-mapping>
        <filter-name>struts2</filter-name>
        <url-pattern>*.action</url-pattern>
    </filter-mapping>
  <!-- spring监听器 -->
  <listener>
      <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>
   
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
</web-app>

4、添加Action

注解@Action  不需要spring 扫描   Action会被struts2-spring-plugin交由spring上下文中管理  因为struts2的Action中有很多类变量,所以是多例的

@ParentPackage("basePackage")
@Namespace("/")
@Action(value = "userAction")
public class UserAction 
{
    private static final Logger logger = Logger.getLogger(UserAction.class);
    
    
    private UserServiceI userService;
    
    public UserServiceI getUserService() {
        return userService;
    }

    /**
     * spring注入
     */
    @Autowired
    public void setUserService(UserServiceI userService) {
        this.userService = userService;
    }
    
    public void test()
    {
        logger.info("Action test测试");
    }

    /**
     * 动态请求
     */
    public void test2()
    {
        logger.info("Action test2测试");
    }
    
    /**
     * 通过上下文获取service
     */
    public void test3()
    {
        logger.info("Action test3测试");
        //获取上下文(spring的上下文)
        ApplicationContext ac = WebApplicationContextUtils.getWebApplicationContext(ServletActionContext.getServletContext());
        //获取注入被扫描的service
        UserServiceI userService1 = (UserServiceI)ac.getBean("userService");
        userService1.test();
    }
    
    /**
     * 通过注入获取service
     */
    public void test4()
    {
        logger.info("Action test4测试");
        userService.test();
    }
}

5、测试

先maven install  然后tomcat部署启动  这个顺序不能反了;

在浏览器输入:

http://localhost:8080/sshe/userAction!test.action

http://localhost:8080/sshe/userAction!test2.action

看控制台输出:

[sy.action.UserAction]Action test测试
[sy.action.UserAction]Action test2测试

上下文获取service

http://localhost:8080/sshe/userAction!test3.action

spring注入service

http://localhost:8080/sshe/userAction!test4.action

原文地址:https://www.cnblogs.com/cac2020/p/5226210.html