zoukankan      html  css  js  c++  java
  • 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-allstruts-2.3.31appsstruts2-blank.war文件解压,之后将struts-2.3.31-allstruts-2.3.31appsstruts2-blankWEB-INFlib下的所有jar发布包拷贝到My-SSH项目的WebContentWEB-INFlib下。

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

    • 在My-SSH项目的WebContentWEB-INFweb.xml中添加filter

      从struts-2.3.31-allstruts-2.3.31appsstruts2-blankWEB-INFweb.xml中可以找到struts2的filter配置信息,并把filter配置信息拷贝到My-SSH项目的WebContentWEB-INFweb.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-allstruts-2.3.31appsstruts2-blankWEB-INFclassesstruts.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-allstruts-2.3.31libstruts2-spring-plugin-2.3.31.jar拷贝到My-SSH项目的WebContentWEB-INFlib中(,否则会抛出异常Action class [userAction] not found - action)。

      从地址http://repo1.maven.org/maven2/org/aspectj/aspectjweaver/下载最新的aspectjweaver-1.8.9.jar,并拷贝到My-SSH项目的WebContentWEB-INFlib中,否则在运行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>
  • 相关阅读:
    jQuery插件开发入门
    [转]JS学习总结-技巧、方法、细节
    JS无法获取display为none的隐藏元素的宽度和高度的解决方案
    vuejs2.0运用原生js实现简单的拖拽元素功能
    HTML5效果:Canvas 实现圆形进度条并显示数字百分比
    git常用命令总结以及用github来展示你的前端页面
    jQuery 对AMD的支持(Require.js中如何使用jQuery)
    vue+springboot上传和下载附件功能
    springboot+vue实现文件上传
    Spring boot+Vue全栈开发---Spring Boot文件上传
  • 原文地址:https://www.cnblogs.com/yy3b2007com/p/6831538.html
Copyright © 2011-2022 走看看