Struts 2 implements the interception operation method of action request object
The core function of struts 2 is action. For developers, using struts 2 is mainly to write action. Action classes usually implement com opensymphony. xwork2. Action interface and implement the execute () method in the interface.
The method is as follows:
public String execute() throws Exception
Struts 2 does not require all action classes written to implement the action interface. You can also directly write a common Java class as an action, as long as you implement a parameterless public method with a return type of string:
public String xxx()
Enter the text:
Create an interceptor object. When a client requests to access the action object, the current interceptor object will be triggered to filter the current request data.
A login interface is established for user name and password input. Before the data in the form object in the login interface is submitted to the action object, it will be intercepted by the interceptor object, and the interceptor object will obtain the registration information from the session object, Judge whether the current user has permission to access the action class object by whether the registration information registerMessage is empty. If registerMessage is null, the current user must register the user information first, add the registerMessage attribute variable to the session object on the registration page, and then log in, Access the action object. An interceptor object is established to intercept all request data accessing the action object.
1: Create an interceptor object myinterceptor, which inherits the abstract interceptor object class.
2: After the interceptor object is established, if you want to use it, you must first register the interceptor object. The specific methods are as follows:
It's in struts Interceptors tag is used in XML to register interceptors
3: To bind the currently registered interceptor object with the specified action class, use interceptor
Tag object to bind its action class with the specified interceptor object
4: Note that if only the specified interceptor object is bound, struts - default Default interception in XML objects
The default object is bound with an interceptor stack, which has more than 20 interceptor pairs
Like, so you must update the original struts - default The interceptor in the XML file is used for rebinding
In the user-defined interceptor object, perform permission interception on the action object:
If users want to log in to access the action object, they must first register a registerMessage information into the session object, otherwise they are requesting access
The action object will be intercepted by the interceptor object. If it is found that the registration information obtained in the current session is empty, it will be returned to the registration error
Go to the failed page
Login interface:
Registration interface:
Action class instance object:
Interceptor object:
In order to use the interceptor object, it must be configured in the configuration file before it works
struts. XML configuration file
Login success interface:
Login failure interface:
Log off the current user information interface:
Running results of the program:
Login interface
Registration interface
Login success interface:
If the user is not registered, log in directly
summary
The above is the method of intercepting the action request object with struts 2 introduced by Xiaobian. I hope it will help you. If you have any questions, please leave me a message and Xiaobian will reply to you in time. Thank you very much for your support for the programming tips website!