Struts2的使用以及Spring整合Struts2

一、如何单独使用Struts2

(1)引入struts2的jar包

commons-fileupload-1.2.1.jar

freemarker-2.3.15.jar

ognl-2.7.3.jar

struts2-core-2.1.8.jar

xwork-core-2.1.6.jar

(2)在项目的src下引入struts.xml文件

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.1.7//EN"
    "http://struts.apache.org/dtds/struts-2.1.7.dtd">

<struts>

    <package name="hello1" extends="struts-default">
        <action name="hello" class="org.tarena.action.Hello">
            <result>
                success.jsp
            </result>
        </action>
    </package>

</struts>

(3)在项目的web.xml文件下进行struts2的配置

<!-- struts2核心filter -->
  <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>/*</url-pattern>
  </filter-mapping>

(4)在项目中编写Action

package org.tarena.action;

public class Hello {
    
    //output
    private String name;
    
    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String execute(){
        name = "陈驰";
        return "success";
    }
}

(5)写两个jsp,hello.jsp跳转到success.jsp

hello.jsp:

<body>
    <a href="/spring004/hello.action">Hello</a>
 </body>

success.jsp:

 <body>
      <p>
          ${name },你好!!!
      </p>
  </body>

最终的效果,点击hello,跳转到success.jsp,显示“陈驰,你好!!!”

 

二、如何用Spring整合Struts2

(1)在刚才的基础上,还需引入新的jar包

struts2-spring-plugin-2.1.8.jar

spring-web-3.2.8.RELEASE.jar

commons-logging.jar

spring-beans-3.2.8.RELEASE.jar

spring-context-3.2.8.RELEASE.jar

spring-core-3.2.8.RELEASE.jar

spring-expression-3.2.8.RELEASE.jar

(2)在刚才的基础上,引入Spring的配置文件applicationContext.xml,放在src下:

<?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:context="http://www.springframework.org/schema/context" 
    xmlns:jdbc="http://www.springframework.org/schema/jdbc"  
    xmlns:jee="http://www.springframework.org/schema/jee" 
    xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:aop="http://www.springframework.org/schema/aop" 
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xmlns:util="http://www.springframework.org/schema/util"
    xmlns:jpa="http://www.springframework.org/schema/data/jpa"
    xsi:schemaLocation="
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd
        http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.2.xsd
        http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.2.xsd
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
        http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsd
        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
        http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.2.xsd">
    
    <bean id="helloBean" class="org.tarena.action.Hello" scope="prototype">
    </bean>
    
</beans>

(3)struts.xml的配置文件中的<action>需要改一下,这里需要注意一个问题,将class属性和Spring容器中的
<bean>元素的id属性保持一致。即原来是利用struts自己去创建一个action,现在是通过整合包
利用class值当作id标识去Spring容器获取Bean对象。

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.1.7//EN"
    "http://struts.apache.org/dtds/struts-2.1.7.dtd">

<struts>

    <package name="hello1" extends="struts-default">
        <!-- 利用struts-spring-plugin.jar去spring容器寻找bean对象
             寻找的规则:利用class属性当作id值去spring容器中获取 -->
        <action name="hello" class="helloBean">
            <result>
                success.jsp
            </result>
        </action>
    </package>

</struts>

 (4)最后还要在web.xml中配置一下,添加ContextLoaderListener,在服务器启动时,实例化Spring容器对象。

为什么要实例化Spring容器对象呢?因为原来是创建action,现在是到Spring容器中寻找了,不能找的时候还没实例化,那就找不到了。

 <!-- 指定Spring配置文件位置和名称 -->
  <context-param>
      <param-name>contextConfigLocation</param-name>
      <param-value>classpath:applicationContext.xml</param-value>
  </context-param>
  
  <!-- 在tomcat启动时,实例化Spring容器对象,给struts-spring-plugin.jar使用
                     就不用写ApplicationContext ac = new ... -->
  <!-- listener在tomcat启动时加载 -->
  <listener>
      <listener-class>
          org.springframework.web.context.ContextLoaderListener
      </listener-class>
  </listener>
  
  
  <!-- struts2核心filter -->
  <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>/*</url-pattern>
  </filter-mapping>

实现的效果,和直接用Struts2一样,说白了,整合之后,就是不让struts2自己创建action了,要在spring容器中找。

posted @ 2016-05-25 17:01  DarrenChan陈驰  阅读(420)  评论(0编辑  收藏  举报
Live2D