Spring integrates Struts2 in two ways

Source: Internet
Author: User
Tags i18n

Spring provides a contextloaderlistener that implements the Servletcontextlistener interface. This class can be used as listener, and it will automatically find web-inf/when it is created The Applicationcontext.xml file below, so if there is only one profile and the configuration file is named Applicationcontext.xml, simply add the following configuration fragment to the Web. xml file:

<!-- 使用ContextLoaderListener初始化Spring容器 -->    <listener>        <listener-class>org.springframework.web.context.ContextLoaderListener        </listener-class>    </listener>

If more than one configuration file needs to be loaded, consider using <context-param.../> elements to determine the file name of the configuration file. , when Contextloaderlistener loads, it looks for an initialization parameter named Contextconfiglocation, so the configuration <context-param.../> should specify a parameter named Contextconfiglocation.

<?xml version= "1.0" encoding= "GBK"?><web-app 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/ Java ee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd " version=" 3.0">            <context-param>       <param-name>Contectconfiglocation</param-name>        <param-value>/web-inf/daocontext.xml,/web-inf/applicationcotext.xml</param-value>    </context-param>    <!--Initialize Spring container with Contextloaderlistener --    <listener>        <listener-class>Org.springframework.web.context.ContextLoaderListener</listener-class>    </listener> </Web-app>

Spring creates the Webapplicationcontext object from the configuration file and saves it in the ServletContext of the Web App. If you want to get an ApplicationContext instance in your app, you can
Get it as follows:

WebApplicationContext ctx=WebApplicationContextUtils.getWebApplicationContext(servletContext)

Let the Spring Management controller

When STRUTS2 forwards the request to the specified action, the action in Struts2 is just a puppet, he is just a codename, does not specify the actual implementation class, and of course it is not possible to create an action instance, Two hidden under the action is the action instance in the spring container, which is the controller that really handles the user request.

Where Struts2 is just a pseudo controller, the functionality of this pseudo-controller is actually done by the controller in the spring container, which enables the core controller to invoke the action in the spring container to handle the user request. Under this strategy, the action that handles user requests is created by the spring plug-in, but when the spring plug-in creates an action instance. Instead of creating the action instance with the class attribute specified when configuring the action, the corresponding bean instance is taken out of the spring container to complete the creation.
Xml

<?xml version= "1.0" encoding= "GBK"?><web-app 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/ Java ee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd " version=" 3.0">            <!--Initialize Spring container with Contextloaderlistener --    <listener>        <listener-class>Org.springframework.web.context.ContextLoaderListener</listener-class>    </listener>    <!--define the filterdispathcer of Struts 2    <filter>        <filter-name>Struts2</filter-name>        <filter-class>Org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>    </filter>    <!--Filterdispatcher is used to initialize Struts 2 and handle all Web requests. -    <filter-mapping>        <filter-name>Struts2</filter-name>        <url-pattern>/*</url-pattern>    </filter-mapping></Web-app>

Applicationcontext.xml

<?xml version= "1.0" encoding= "GBK"?><!--The root element of the spring configuration file, using spring-beans-3.0.xsd semantics constraints--<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns= "Http://www.springframework.org/schema/beans" xsi:schemalocation="Http://www.springframework.org/schema/beans http://www.springframework.org/ Schema/beans/spring-beans-3.0.xsd ">            <!--Define a business logic component that implements a class of Myserviceimp--    <bean id="MyService"class="Com.bh.service.impl.MyServiceImpl" />             <!--Let Spring manage the action instance because each action contains the state information of the request, so you must configure scope not to be a singleton--    <bean id="Loginaction" class="Com.bh.action.LoginAction"  Scope="Prototype">                <!--Dependency Injection business logic components --        < property name="MS" ref="MyService"/>    </Bean></Beans>

Struts.xml

<?xml version= "1.0" encoding= "GBK"?><!--to specify DTD information for struts 2 configuration files--<! DOCTYPE struts Public "-//apache software foundation//dtd struts Configuration 2.1.7//en" "http://struts.apache.org/ Dtds/struts-2.1.7.dtd "><!--The root element of the Struts 2 configuration file--<struts>    <!--configured with series constants --    <constant name="struts.i18n.encoding" value="GBK"/>     <constant name="Struts.devmode" value="true"/>      < package name="Lee" extends= "struts-default">         <!--defines the action that handles the user request, the class property of the action is not the actual processing class, but the bean instance in the Spring container- -        <action name="Loginpro" class="Loginaction">            <!--Configure view pages for two logical views--            <result name="error">/web-inf/content/error.jsp</result>            <result name="Success">/web-inf/content/welcome.jsp</result>        </Action>        <!--to list all view pages when you access the app directly --        <action name="*">            <result>/web-inf/content/{1}.jsp</result>        </Action>    </Package ></struts>

Using Automatic assembly

By setting the Struts.objectFactory.spring.autoWire constant, you can change the spring plug-in amount automatic assembly policy, which can accept several values as follows:

    • Name: Automatically assembled according to the attribute name. The spring plug-in looks for all the beans in the container, finds the bean with the same id attribute as the business logic component required by the action, and injects the bean instance into the action instance.
    • Type: Automatically assembled according to the attribute type. The spring plug-in looks for all the beans in the container and finds the bean whose type is exactly the same as the business logic component required by the action, injecting the bean instance into the action instance.
    • The Auto:spring plugin automatically detects which automatic assembly method is required.
    • Constructor: Similar to type, the difference is that Constructor uses constructors to construct the injected required parameters instead of using the Set value injection method.

Xml

<?xml version= "1.0" encoding= "GBK"?><web-app 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/ Java ee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd " version=" 3.0">                <listener>        <listener-class>Org.springframework.web.context.ContextLoaderListener</listener-class>    </listener>        <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>

Applicationcontext.xml

<?xml version= "1.0" encoding= "GBK"?><!--The root element of the spring configuration file, using spring-beans-3.0.xsd semantics constraints--<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns= "Http://www.springframework.org/schema/beans" xsi:schemalocation="Http://www.springframework.org/schema/beans http://www.springframework.org/ Schema/beans/spring-beans-3.0.xsd ">            <!--Define a business logic component that implements a class of Myserviceimp--    <bean id="MS" class="Com.bh.service.impl.MyServiceImpl" /></Beans>

Struts.xml

<?xml version= "1.0" encoding= "GBK"?><!--to specify DTD information for struts 2 configuration files--<! DOCTYPE struts Public "-//apache software foundation//dtd struts Configuration 2.1.7//en" "http://struts.apache.org/ Dtds/struts-2.1.7.dtd "><!--The root element of the Struts 2 configuration file--<struts>    <!--configured with series constants --    <constant name="struts.i18n.encoding" value="GBK"/>      <constant name="Struts.devmode" value="true"/>    < package name="Lee" extends= "struts-default">        <!--define action to handle user requests--        <action name= "Loginpro"class=" Com.bh.action.LoginAction ">                        <!--Configure view pages for two logical views--            <result name="error">/web-inf/content/error.jsp</result>            <result name="Success">/web-inf/content/welcome.jsp</result>        </Action>        <!--to list all view pages when you access the app directly --        <action name="*">            <result>/web-inf/content/{1}.jsp</result>        </Action>    </Package ></struts>

Spring integrates Struts2 in two ways

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.