Spring and Struts Integration

Source: Internet
Author: User

1. Add the struts2 package

2. Configure the struts2 filter in Web. xml

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

3. Write struts. xml and test the application to verify that the struts configuration is correct.

<package name="h" extends="struts-default"> <action name="login" class="org.ymm.actions.LoginAction" method="mlogin"><result name="success" type="redirect">login.jsp</result><result name="fail">fail.jsp</result></action></package>

4. If struts is OK, add the spring support package and configure the listener contextloaderlistener.
(Let spring be loaded before struts and manage struts actions later)

Note: struts2-spring-plugin-2.3.3.jar must be added to this pack

<context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:beans.xml</param-value> </context-param>  <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener>

The loading sequence of Web. XML is context-Param.
-> Listener-> filter-> servlet (because listener is prior to filter, use listener to start spring)

See: http://blog.csdn.net/without0815/article/details/7689661

In the struts2 configuration file, add
<Constant name = "struts. objectfactory" value = "Spring"> </constant> attribute


5. Compile beans. xml beans, such as beans that manage actions in sturts:

<bean id="spring" class="org.ymm.actions.LoginAction">.......</bean>


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.