Struts2 18 Interceptor (20)

Source: Internet
Author: User

Summary: The Interceptor is a very important component of struts2 and an extremely subtle part of struts2. Many functions of struts2 are implemented through these interceptors, such as exception handling, file Upload request parameter assignment, data verification, and other functions are implemented in the interceptor. To thoroughly understand the interceptor, it is critical to understand the execution process of the Interceptor. This requires a better understanding of some important classes involved in the execution process of the interceptor, especially ActionContext, ValueStack, and ActionInvocation, these classes run through the entire execution process of struts2. Only by understanding this can we better understand the code in each interceptor. The positions of some interceptor in the interceptor Stack are not random and there is a certain foundation to follow. This is determined by the specific functions of these Interceptor. For example, the ExceptionMappingInterceptor interceptor must be placed on the top of the stack, this interceptor is returned only when it is placed on the top of the stack. This interceptor can contain the execution logic of all other interceptors and actions. If an exception is thrown, it can be captured in the ExceptionMappingInterceptor interceptor, then, the exception is processed in a centralized manner. The ModelDrivenInterceptor interceptor should be placed before ParametersInterceptor, because the ModelDrivenInterceptor interceptor will push the model into the value stack, the value can be assigned to the model where the ModelDrivenInterceptor interceptor is pushed into the value stack only when the ParametersInterceptor interceptor sets the request parameters before ParametersInterceptor. DefaultWorkflo The wInterceptor interceptor must be placed at the bottom of the stack, because the interceptor is used to detect whether there are any errors in the Action execution process. Of course, it must be put to the end for detection, if an error occurs, the system will navigate to the specified Result. Of course, there are still some interceptor locations not mentioned, so let's try it by yourself ...... I have written so many articles about interceptor, because it is mainly used to explain the source code, so I didn't talk about how to customize an interceptor. At the end of this article, I will give an example of a custom interceptor, loginInterceptor ). The following is the interceptor configuration: [html] <interceptors> <interceptor name = "login" class = "com. xforwarfjpk. action. loginInterceptor "> <param name =" excludeMethods "> loginUI, login </param> </interceptor> <interceptor-stack name = "defaultStack"> <interceptor-ref name = "login"/> <interceptor-ref name = "defaultStack "/> </interceptor-stack> </interceptors> A parameter is configured for LoginInterceptor, indicates that the loginUI and login methods do not apply the interceptor, because the interceptor is used to determine whether to log on, while the loginUI and logi The n methods are used to display the user logon interface and process user logon operations. If the two methods are blocked, the user will never log on, therefore, LoginInterceptor inherits from the MethodFilterInterceptor class because the two methods used to display the logon interface and process user logon operations cannot be blocked. In the preceding configuration, an interceptor stack is created, but its name is also set to defaultStack. It is mainly used to avoid specifying the name of the interceptor stack in the Action configuration. The following is the source code of the custom Interceptor: [java] package com. xforwarfjpk. action; import com. opensymphony. xwork2.ActionInvocation; import com. opensymphony. xwork2.interceptor. extends; public class LoginInterceptor extends MethodFilterInterceptor {private static final long serialVersionUID = 1L; @ Override protected String doIntercept (ActionInvocation invocation) throws Exception {// get the user Object user = in session Vocation. getInvocationContext (). getSession (). get ("com. xforwarfjpk. user "); if (null = user) {// if the user is null, it indicates that the user has not logged on to the logon interface. String resultName =" loginUI "; return resultName ;} // if the user logs on, the next interceptor return invocation is called. invoke () ;}} all the interceptors have been explained. Basically, all the interceptors in defaultStack are described in detail, hoping to help you. If there are any errors, please correct them!

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.