Description of the token interceptor of struts2

Principle: The token interceptor of struts2 is an interceptor for repeated submissions. The implementation is: adding a token tag to the form, as follows:

 

1     <form action="user" method="post">
2         name:<input name="name">
3         age:<input name="age">
4         <input type="submit" value="add">
5         <s:token></s:token>
6     </form>

 

A hidden tag will be found in the static page returned by the server, and a random code will be generated in this tag, which will be added to the session. When the entire page is submitted, it will be deleted in the session. When submitting, this object cannot be found in the session, thereby avoiding repeated submissions.

The configuration in struts.xml is as follows:

    <package name="test" namespace="/" extends="struts-default">    
    <action name="input" class="com.wbs.InputAction">
            <result>/input.jsp</result>                        
        </action>
        <action name="user" class="com.wbs.UserAction">
            <result>/addOK.jsp</result>            
            <interceptor-ref name="defaultStack"></interceptor-ref>
            <interceptor-ref name="token"></interceptor-ref>
            <result name="invalid.token">/error.jsp</result>
        </action>
    </package>
When visiting, first visit the input page, enter the data to be submitted in this page, and you will find the corresponding action to process. However, it should be noted that the <intercptor-ref name="defaultStack"/> must be before the <interceptor-ref name="token"/>, because the token is processed after the default loading is completed. In the <interceptor-ref name="token"/> After interceptor-ref name="token"/>, add <result name="invalid.token">/xxx.jsp</result>. Because when the token interceptor intercepts a page, it will find the action of invalid.token by default, and hand over the processing result to the aciton for processing.

Guess you like

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