Ssh2 -- struts2 interceptor

Source: Internet
Author: User

Although I have never learned struts1, I learned that struts1 does not have an interceptor. It is only available in Struts2. It is developed based on WebWork. As the name suggests, when it comes to interceptors, we will surely think of it as intercepting things and serving as a restriction. Well, what is it blocking here? In struts2, the interceptor is used to intercept actions. Before an Action is executed, the interceptor plays a certain role. It executes some pre-processed code and then executes the related methods in the Action. Then, it will return to the interceptor and perform subsequent operations. I just learned this. If I do not understand it, please leave a message to correct it.


Struts2 interceptor Principle

The implementation principle of the Struts2 interceptor is relatively simple. When requesting the Action of struts2, Struts2 searches for the configuration file, instantiates the relative interceptor object according to its configuration, and then concatenates it into a list, finally, the interceptor in the list is called one by one.

Define interceptor

Public class MyInterceptor implements Interceptor {@ Overridepublic void destroy () {System. out. println ("-- destroy () --") ;}@ Overridepublic void init () {System. out. println ("-- init () --") ;}@ Overridepublic String intercept (ActionInvocation invocation) throws Exception {System. out. println ("-- intercept () --"); Map
 
  
Session = invocation. getInvocationContext (). getSession (); if (session. get ("username ")! = Null) {return invocation. invoke () ;}else {return "error ";}}} // when you need to intercept the method specified in the action, the required handler // public class MyInterceptor extends MethodFilterInterceptor {public class MyInterceptor extends AbstractInterceptor {@ Overridepublic String intercept (ActionInvocation invocation) throws Exception {System. out. println ("-- intercept () --"); Map
  
   
Session = invocation. getInvocationContext (). getSession (); if (session. get ("username") = null) {return "error";} else {return "user ";}}}
  
 

Note: The Custom interface must inherit the class of AbstractInterceptor, com. opensymphony. xwork2.interceptor. the Interceptor interface declares three methods, including init, destroy, and intercept methods. Among them, the init and destroy methods are executed each time at the beginning and end of the program, whether the interceptor is used or not. xml indicates that the Struts2 interceptor will be executed. The intercept method is the body of the interception, and the logic is executed every time the interceptor takes effect.

However, struts also provides abstract classes to simplify this step. Public class MyInterceptor extends MethodFilterInterceptor and public class MyInterceptor extends actinterceptor. AbstractInterceptor provides the empty implementations of init () and destroy (). You only need to overwrite the intercept () method; methodFilterInterceptor provides the includeMethods and excludeMethods attributes to filter the Action method that executes the filter. You can use param to add or exclude methods to be filtered.

Register interceptor

 
  
  
   
   (Must be added; otherwise, an error occurs)
  
 

Configure interceptor

All configurations are in struts. in xml, the following is a small example. Note: when using the interceptor, the default stack defaultStack of the interceptor provided by struts2 must be referenced at the end of the Action.

 
  
   
   
    
    (Must be added; otherwise, an error occurs)
   
  (This sentence sets the interceptor stack automatically called by all actions)
  
  
   
/User. jsp
  
  
   
/Error. jsp
  
  
 

Note: The default stack defaultStack of the interceptor provided by struts2 must be referenced at the end of the Action mentioned above. I am not very clear about it either.

Chu, but I will further understand this knowledge point later.

In this way, our interceptor can be used. This is only a basic part of the Interceptor. It can be implemented simply. Just getting started with ssh, we also need to fill in this part of Learning Through summarization, remember that there is a certain relationship and difference between it and filter in the video. At that time, we did not carefully consider it. Next we will compare the difference between it and interceptor through studying filter.


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.