Struts2 custom interceptor Development

Source: Internet
Author: User

Struts2 custom interceptor Development
Introduction

In the previous article has talked about the basic concepts of interceptor (http://blog.csdn.net/xlgen157387/article/details/45951163), below we implement a custom interceptor together.

Interceptor Interface
public interface Interceptor extends Serializable {    /**     * Called to let an interceptor clean up any resources it has allocated.     */    void destroy();    /**     * Called after an interceptor is created, but before any requests are processed using     * {@link #intercept(com.opensymphony.xwork2.ActionInvocation) intercept} , giving     * the Interceptor a chance to initialize any needed resources.     */    void init();    /**     * Allows the Interceptor to do some processing on the request before and/or after the rest of the processing of the     * request by the {@link ActionInvocation} or to short-circuit the processing and just return a String return code.     *     * @param invocation the action invocation     * @return the return code, either returned from {@link ActionInvocation#invoke()}, or from the interceptor itself.     * @throws Exception any system-level error, as defined in {@link com.opensymphony.xwork2.Action#execute()}.     */    String intercept(ActionInvocation invocation) throws Exception;}

It can be seen that only three methods need to be implemented in the interface.

Implement the Interceptor Interface
Public class MyInterceptor implements Interceptor {public void destroy () {System. out. println ("destroy () ----");} public void init () {System. out. println ("init () ----");} public String intercept (ActionInvocation invocation) throws Exception {System. out. println ("before the Action is executed"); String result = invocation. invoke (); System. out. println ("after Result execution"); return result ;}}

We need to configure in the struts. xml file:


          
               
            
    
           
   test            
   
    /welcome.jsp
               
               
            
    
       
  

First declare a custom Interceptor:


              
           
   
  

Then reference in action:


  

The execution results can be imagined:

Before the Action is executed, account = test, password = test, submitFlag = login after the Result is executed

Related Article

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.