Interceptors are programmed on a tangent plane to call a method before your service or a method, or invoke a method after the method, such as a dynamic proxy is a simple implementation of the Interceptor, the filter is in the Java Web, you incoming request, Response filter out some of the information in advance, or set some parameters in advance, and then pass into the servlet or struts action for business logic, such as filtering out illegal URLs (no
Simple use of struts2 interceptor
Login. jsp page body
Show. jsp page body
After logging on to the page with the permission:
---
The show. jsp page is displayed only when the username is admin and the password is admin. Otherwise, the login. jsp page is returned.
The LoginAction class is a simple login action.
public class LoginAction extends ActionSupport{private String user
The struts-default.xml defines a series of interceptor and interceptor chains
A default interceptor defastack stack is also defined. Once the default interceptor is defined,
This interceptor will take effect for all the actions in the package. Of course, if your Action expli
ParametersInterceptor the interceptor is at the position of defaultStack 15th. Here we skip an interceptor. First we will talk about ParametersInterceptor, and then we will talk about the third Interceptor. Because the blocker inherits from ParametersInterceptor, but the value assignment parameter source is different, it is easy to understand ActionMappingParamet
When you are working on a project today, use the Interceptor to check user permissions. There is no problem with the interceptor, but permission interception is implemented, but all passed parameters are invalid. After a long time, I have studied the internal mechanism of the interceptor.Struts2. The reason is found:Give an ActonAfter configuring a custom interceptorActionThe default
The Interceptor function is similar to the filter that we do not develop based on the Strtus framework.
Configure the default interceptor, our interceptor here is named Defaultstack
The code is as follows
Copy Code
Declaring interceptors, here's the name of the interceptor we're
Interceptor/interceptor
Name
Description
Alias
Alias
The request parameters are converted between different requests, and the request content remains unchanged.
Chaining
Chain
This allows the attribute of the previous action to be accessed by the latter action. It is now used in combination with the result () of the chain type.
Checkbox
Checkbox
Added the c
After the above introduction, we can see that the configuration elements related to the interceptor are:
-Stack/> element: used to define the interceptor stack. The element contains multiple
Stack/> and
-Ref/>: This element configures the default interceptor for the specified package. This element is used as a child element of the
The preceding ele
The AnnotationValidationInterceptor interceptor is in the defaultStack 17th position and is mainly used for data verification. The interceptor inherits from the ValidationInterceptor interceptor and adds the function of canceling verification using Annotations on methods. ValidationInterceptor inherits from MethodFilterInterceptor. MethodFilterInterceptor is not
Interceptors are objects that dynamically intercept action calls. It provides a mechanism that allows developers to define code that executes before and after an action is executed, and prevents it from executing before an action is executed, as well as providing a way to extract the reusable part of the action.
In AOP (aspect-oriented programming) interceptors are used to intercept a method or field before it is accessed, and then add some action before or after.
The
The rights interceptor is as follows:
/** *
Rights blocker
* block non-logged-in users * @author Administrator * * * * * * * public
class Authorinterceptor extends abstractinterceptor{
@Override public
String Intercept (actioninvocation arg0) throws Exception {
// If it is a login request, the direct release of
Object o = arg0.getaction ();
System.out.println (O.getclass ());
if (o instanceof loginaction) {
return Arg0.invoke ();
MultiselectInterceptor the interceptor processes the defaultStack 12th position, which is used to process the select tag where no option is selected. The logic of this interceptor is very similar to that of the CheckboxIntercept interceptor, which is almost identical. When no select tag (multiple = "multiple") is selected, the select tag is not selected, which is
Permission control is used by many applications, and this example requires the user to log on and view a view resource in the system for the specified user name, otherwise the system goes directly to the login page. Check whether the user is logged in, usually by tracking the user's session to complete, through the Actioncontext can access to the properties in the session, Interceptor Intercept (Actioninvocation invocation) The invocation parameter of
This feature is very important for the user experience. Implementation is actually very simple.
The code for the Interceptor is as follows:
Package go.derek.advice;
Import Go.derek.entity.User;
Import Go.derek.util.CommonChecks;
Import go.derek.util.Constant;
Import Java.util.Map;
Import Javax.servlet.http.HttpServletRequest;
Import Org.apache.struts2.StrutsStatics;
Import org.springframework.beans.BeansException;
Import Org.springframework.contex
Problem domain: the login user has not interacted with the server for a long time. If the operation is performed again, the system prompts "timeout". Please log on again.
Solution: session timeout settings and struts interceptor intercept interactive operations
Implementation
Use web. xml configuration to set session Timeout
Struts interceptor code implementation
Public class SessionIterceptor extends
1.SpringMVC has a unified entrance Dispatcherservlet, all requests are through Dispatcherservlet.
Dispatcherservlet is the predecessor controller, configured in the Web.xml file. To intercept a matching request, the servlet interception matching rule has to be defined, and the intercepted request is distributed to the target controller according to certain rules. So we now add the following configuration to the Web.xml:
An interceptor for filteri
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.