Struts 2 Running Process

1. Use Structs core filter to intercept all requests in web.xml.

<filter>
        <filter-name>struts2</filter-name>
        <filter-class>org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter</filter-class>
    </filter>
    <filter-mapping>
        <filter-name>struts2</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

 

 

 

 

2. The core filter finds the specified action according to the name in structs.xml, and calls the interceptor and the interceptor stack of the action to preprocess the request.

<package name="action" namespace="/" extends="struts-default">
        <action name="loginAction" class="action.LoginAction">
            <result name="teacher">/teacher.jsp</result>
            <result name="student">/student.jsp</result>
            <result name="error">/error.jsp</result>
        </action>
    </package>

 

There are two ways to configure action. One is to configure it in structs.xml, the other is to use conventions, which correspond to the XxxAction class. The conventions need plug-in support.

 

Servlet preprocesses requests using Filter and Action preprocesses requests using Interceptor. A group of interceptors form an interceptor stack.

The interceptor can be configured in < action > or < package > and configurable in < action > is only valid for the current action, while configurable in < package > is valid for all actions under the whole < package >.

 

The configuration of <package> inherits the configuration of <package name="struts-default"</package> in structs-default.xml.

When the default interceptor is configured, the default interceptor stack in the structs-default package is automatically called for processing.

 

 

 

3. Create an instance of the action by reflection (calling the constructor of the null parameter), and then call the setter method of the action to assign the request parameters to the member variables.

 

 

4. According to the method attribute in <Action>, call the method specified by this Action to process the request and return a value of String type.

The default method attribute is execute by default.

<action name="loginAction" class="action.LoginAction" method="">
            <result name="teacher">/teacher.jsp</result>
            <result name="student">/student.jsp</result>
            <result name="error">/error.jsp</result>
        </action>

 

 

 

5. According to the <result> configured in <action> and the string returned, the page to be invoked is determined.

 

 

6. According to the configuration in structs.xml, the page is called and returned to the browser as a response.

<action name="loginAction" class="action.LoginAction">
            <result name="teacher" type="redirect">/teacher.jsp</result>
            <result name="student" type="dispatcher">/student.jsp</result>
            <result name="error">/error.jsp</result>
</action>

You can configure it one by one in < result > or you can configure it uniformly under < package >.

<package name="action" namespace="/" extends="struts-default">
        <result-types>
            <result-type name="redirect" class="org.apache.struts2.result.ServletRedirectResult" default="true"/>
        </result-types>
        <action name="loginAction" class="action.LoginAction">
            <result name="teacher">/teacher.jsp</result>
            <result name="student">/student.jsp</result>
            <result name="error">/error.jsp</result>
        </action>
    </package>

This configuration needs to be set to the default value to be valid default="true".

Note: <package> requires the order of child elements.

 

If there is no configuration, the configuration in the inherited struct-default is invoked (default is dispatcher, forwarding).

Tags: Java JSP xml Struts Apache

Posted on Tue, 03 Sep 2019 02:46:22 -0700 by pullaratt