Spring Learning (eight) Spring integration struts2

Source: Internet
Author: User

The integration principle of spring framework on struts and other presentation frameworks:
Use spring's IOC container to manage the action in struts for processing requests
Configure an action as a bean in an IOC container

Extension: Spring's integration Principle (encapsulation) of the persistence layer Framework/technology:
Provides a template-class package for the corresponding Technology/framework development process
Through the use of template classes, to achieve the traditional development process of "substitution."
Second, the integration method:
Plug-in mode
STRUTS2 also provides a plug-in configuration file to implement the Spring framework integration Struts-plugin.xml
Struts2-spring-plugin.jar
Third, the integration process
A Create a Web project, add STRUTS2 Framework support (import STRUTS2 to bring the required package and configure Filter in Web. xml)

b Web Project Add Spring Framework support (import the spring core layer jar package, and the package under the Struts2 file, configure the Listener Listener and container profile parameters in Web. xml)

Iv. Specific examples
The use of spring in Web engineering portals
Structure:

Page:

 <form action="login.action"method="post">        type="text"  name="loginname"/><br/>        type="password" name="password"/><br/>        type="submit"value="登录"/>    </form>

Xml

<?xml version= "1.0" encoding= "UTF-8"?><web-app version="3.0"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/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd ">                 <filter>    <filter-name>Etoak</filter-name>    <filter-class>Org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>  </filter>  <filter-mapping>    <filter-name>Etoak</filter-name>    <url-pattern>*.action</url-pattern>  </filter-mapping>  <listener>    <listener-class>Org.springframework.web.context.ContextLoaderListener</listener-class>  </listener>  <context-param>    <param-name>Contextconfiglocation</param-name>    <param-value>/web-inf/applicationcontext.xml</param-value>  </context-param>  <display-name></display-name>   <welcome-file-list>    <welcome-file>index.jsp</welcome-file>  </welcome-file-list></Web-app>

Applicationcontext.xml

<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-3.2.xsd ">             <!--each time an action is added to the project to process the request, it needs to be configured as the Bean 1 action in the IOC container when it is instantiated?                When the container starts with struts2 alone, when is the action instantiated?  2 Action instantiation status per client commit request? Single use of struts2 alone, non-singleton note: In order to ensure the consistency of the action State; When you configure an action as a Bean object in an IOC container, you need to set its state to ~ non-singleton state-->
         <bean id="Action" class="Com.etoak.action.LoginAction"  Scope="Prototype">        </Bean></Beans>

Struts.xml

<! DOCTYPE struts Public "-//apache software foundation//dtd struts Configuration 2.3//en" "Http://struts.apache.org/dt Ds/struts-2.3.dtd "><struts>    <!--request resolver Actionproxy "Login" request dispatcher actioninvocation --    < package name="Etoak" extends= "struts-default">        <!--How the mapping between request and processing forms <action Name= "request" class= "Action" > class attribute: Point to an IOC container The ID value of a bean--        <action name="Login" class="Action">            <result name="Success" type="redirect">/success.jsp</result>        </Action>    </Package ></struts>

Loginaction.java

 Public  class loginaction extends actionsupport {    PrivateString LoginName;PrivateString password; PublicStringGetloginname() {returnLoginName } Public void Setloginname(String LoginName) { This. LoginName = LoginName; } PublicStringGetPassword() {returnPassword } Public void SetPassword(String password) { This. Password = password; }@Override     PublicStringExecute()throwsException {//Processing Login.action requests-DAOSystem.out.println ("loginname--"+loginname); System.out.println ("password--"+password);//Invoke DAO IOC Dependency injection (setter injection)        return  This.    SUCCESS; }}

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Spring Learning (eight) Spring integration struts2

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.