Struts 2 and Spring integration

Keywords: Java Struts Spring xml encoding

Preface

This post mainly explains how Spring integrates with struts 2 framework

Key points of integration of struts 2 and Spring:

  • The action object is given to Spring to create

Building environment

Entering the jar package

To import a jar file:

  • 1) Introduce struts. Jar related files
  • 2) Spring core related jar files
  • 3) Spring web support jar package
    • spring-web-3.2.5.RELEASE.jar [spring source code]
    • Struts 2-spring-plugin-2.3.4.1.jar [struts source code]

Write profile

  • struts.xml [struts path and action mapping configuration]
  • bean.xml [spring ioc container configuration]
  • web.xml
    • [core filter: introduce struts function]
    • [initialize the ioc container of spring] view the spring API

web.xml file

web.xml file

In addition to configuring the allocator of struts 2, load the configuration file of Spring

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
         version="3.1">
    <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>


    <!-- 2. spring To configure -->
    <context-param>
        <param-name>contextConfigLocation</param-name>

        
        <param-value>/WEB-INF/classes/bean*</param-value>
    </context-param>
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

</web-app>

Writing a Spring configuration file

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

</beans>

Writing Struts2 configuration file

<?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>
    <package name="aaa" extends="struts-default">

        <action name="bbb" class="userAction">
            <result name="success" >/2.jsp</result>
        </action>

    </package>


</struts>

Last

If there is something wrong in the article, you are welcome to correct it and communicate with each other. Students who read WeChat technology articles and want to get more Java resources can pay attention to WeChat official account: Java3y

Posted by jarv on Fri, 03 Apr 2020 11:58:34 -0700