Spring framework learning (5) spring integration struts2

Content from: spring integration struts2

 

1. The integration principle of the spring framework on the presentation layer framework such as struts: 
use the spring's ioc container to manage the action used to process requests in struts, and 
configure the action as a bean in the ioc container

Extension: Spring's integration principle (encapsulation) of persistence layer framework/technology: 
Provide template class to encapsulate the development process of corresponding technology/framework. 
Through the use of template class, the "replacement" of traditional development process is realized. 


2. Integration method: The 
plug-in method 
struts2 also provides a plug-in configuration file struts-plugin.xml 
struts2-spring-plugin.jar  in order to integrate the spring framework.


Third, the integration process 
a Create a web project, add struts2 framework support (import struts2 comes with required packages and configure Filter in web.xml)

b web project to add spring framework support (import the spring core layer jar package and the package under the struts2 file, configure the Listener listener and container configuration file parameters in web.xml)

 

4. Specific examples 

page:

<form action="login.action" method="post">
        登录名:<input type="text"  name="loginname"/><br/>
        密码:<Input type="password" name="password"/><br/>
        <Input type="submit" value="登录"/>
    </form>

web.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">
    <!-- 
        Every time you add an Action to handle the request in the project
        You need to configure it as a bean in the ioc container

        1 When is the action instantiated? When the container starts
            When using struts2 alone, when is action instantiated?
                Every time the client submits a request
        2 Action instantiation state? Singleton
            When using struts2 alone, non-singleton

        Note: In order to ensure the consistency of the action state;
        When configured as a bean object in the ioc container, its state needs to be set 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/dtds/struts-2.3.dtd">
<struts>
    <!-- 
        Request Parser ActionProxy "login"
        Request dispatcher ActionInvocation
     -->
    <package name="etoak" extends="struts-default">
        <!-- 
            How the mapping between requests and processing is formed
            <action name="请求" class="Action">
            class attribute: points to the ID value of a bean in the ioc container
         -->
        <action name="login" class="action">
            <result name="success" type="redirect">/success.jsp</result>
        </action>
    </package>
</struts>

LoginAction.java

public class LoginAction extends ActionSupport {
    private String loginname;
    private String password;
    public String getLoginname() {
        return loginname;
    }
    public void setLoginname(String loginname) {
        this.loginname = loginname;
    }
    public String getPassword() {
        return password;
    }
    public void setPassword(String password) {
        this.password = password;
    }
    @Override
    public String execute() throws Exception {
        // 处理login.action请求 - DAO
        System.out.println("loginname--"+loginname);
        System.out.println( "password--"+ password);
         // Call DAO ioc dependency injection (setter injection) 
        return  this .SUCCESS;
    }
}

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325168258&siteId=291194637