Spring(四):Spring整合Hibernate,之后整合Struts2

  • 背景:

  上一篇文章《Spring(三):Spring整合Hibernate》已经介绍使用spring-framework-4.3.8.RELEASE与hibernate-release-5.2.9.Final项目整合搭建的过程,本文基于上一篇文章的基础之上,整合Struts2。

  开发环境简介:

    1)、jdk 1.8

    2)、spring-framework-4.3.8.RELEASE、hibernate-release-5.2.9.Final、struts-2.3.31

  • 引入Struts2的jar开发包到新建项目My-SSH中

  将\struts-2.3.31-all\struts-2.3.31\apps\struts2-blank.war文件解压,之后将struts-2.3.31-all\struts-2.3.31\apps\struts2-blank\WEB-INF\lib\下的所有jar发布包拷贝到My-SSH项目的WebContent\WEB-INF\lib\下。

之后,需要查看是否My-SSH项目的WebContent\WEB-INF\lib\中的jar包是否有重复的jar包,如果有重复的jar包,需要删除版本低的jar包(否则,会发生版本冲突)。

  • 在My-SSH项目的WebContent\WEB-INF\web.xml中添加filter

  从\struts-2.3.31-all\struts-2.3.31\apps\struts2-blank\WEB-INF\web.xml中可以找到struts2的filter配置信息,并把filter配置信息拷贝到My-SSH项目的WebContent\WEB-INF\web.xml中,

配置之后web.xml内容如下:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID"
    version="3.0">
    <display-name>My-SSH</display-name>
    
    <welcome-file-list>
        <welcome-file>index.html</welcome-file>
        <welcome-file>index</welcome-file>
    </welcome-file-list>
    
    <!-- 配置启动IOC容器的Listener -->
    <!-- needed for ContextLoaderListener -->
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:applicationContext.xml</param-value>
    </context-param>

    <!-- Bootstraps the root web application context before servlet initialization -->
    <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>
    
</web-app>
  • 将struts2的配置文件struts.xml拷贝到My-SSH项目的conf下

  将\struts-2.3.31-all\struts-2.3.31\apps\struts2-blank\WEB-INF\classes\struts.xml文件拷贝到My-SSH项目的conf下,

并编辑为如下内容:

<?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>
    <constant name="struts.enable.DynamicMethodInvocation" value="false" />
    <constant name="struts.devMode" value="true" />

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

    </package>
</struts>
  • Struts2整合Spring

1)加入Struts2的Spring插件的jar包

  将\struts-2.3.31-all\struts-2.3.31\lib\struts2-spring-plugin-2.3.31.jar拷贝到My-SSH项目的WebContent\WEB-INF\lib\中(,否则会抛出异常Action class [userAction] not found - action)。

  从地址http://repo1.maven.org/maven2/org/aspectj/aspectjweaver/下载最新的aspectjweaver-1.8.9.jar,并拷贝到My-SSH项目的WebContent\WEB-INF\lib\中,否则在运行My-SSH项目时,会抛出异常:nested exception is java.lang.NoClassDefFoundError: org/aspectj/weaver/reflect/ReflectionWorld$ReflectionWorldException。

2)在Spring的配置文件中正常配置Action,注意Action的scope为prototype

  首先,需要在conf下创建一个Spring bean配置文件applicationContext-beans.xml(用来注册存储Struts2的Action类的bean),并在src下新建com.dx.ssh.actions包,在该包下新建Action类EmployeeAction.java

 

package com.dx.ssh.actions;

import com.opensymphony.xwork2.ActionSupport;

public class EmployeeAction extends ActionSupport {
    private static final long serialVersionUID = 1L;

}

在applicationContext-beans.xml文件中添加EmployeeAction的bean.

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
    <bean id="employeeAction" class="com.dx.ssh.actions.EmployeeAction" scope="prototype"></bean>
</beans>

  其次,修改web.xml“配置启动IOC容器的Listener”的配置内容:

    <!-- 配置启动IOC容器的Listener -->
    <!-- needed for ContextLoaderListener -->
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:applicationContext*.xml</param-value>
    </context-param>

3)在Struts2的配置文件struts.xml中配置Action时,class属性指向该Action在IOC中的id

struts.xml配置EmployeeAction的Action时

<?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>
    <constant name="struts.enable.DynamicMethodInvocation" value="false" />
    <constant name="struts.devMode" value="true" />

    <package name="default" namespace="/" extends="struts-default">
        <action name="emp-*" class="employeeAction" method="{1}">
            <result name="list">/WEB-INF/views/emp-list.jsp</result>
        </action>
    </package>
</struts>

 

posted @ 2017-05-09 17:10  cctext  阅读(1081)  评论(0编辑  收藏  举报