, reducing the code of the Action. In this way, the interceptor and Action functions are more simple. When the common function code is encapsulated in the Interceptor (Code modularization), you can configure the interceptor of the corresponding function for different actions according to the function requirements. The reusability of the functions implemented by t
Struts 2 interceptor and struts interceptorWhat is the Struts 2 interceptor?
When a user requests an Action class in the background, the interceptor sends the page (data) to the browser before the execution of the Excute () method of the Action and after the Result returns the magic board attempt) some common operations are required to intercept data in the
timer. First, create the Action class tuotrial/timerinterceptexception. java with the following content:
Packagetutorial;Importcom. opensymphony. xwork2.ActionSupport;Publicclasstimerinterceptexceptionextendsactionsupport {@ OverridePublicString execute (){Try {// Simulate time-consuming operationsThread. sleep (500 );} Catch (Exception e ){E. printStackTrace ();}ReturnSUCCESS;}}
Configure Action, named Timer. The configuration file is as follows:
"-
: --------------------------------------[Result] e42100003458say: Hello World
The above information is the log information output by logginginterceptor. The log information is output in the request and response respectively, as well as the parameter information in the output request and the returned value information in the response.
2. I just added an interceptor to the client. Now we write an interceptor
configuration.See the actioninvocation. Init method for the relevant code: Java code:PrivatevoidInit ()throwsException ... {......List interceptorlist =NewArrayList (Proxy.getconfig (). Getinterceptors ());Interceptors2. Pass actioninvocation. when the Invoke method invokes the action implementation, the Interceptor is executed:Here is theaction dispatch code in defaultactioninvocation :
Two-step configuration Spring MVC Interceptor Interceptor One interceptor first step to implement Handlerinterceptor interface The second step registers into spring
Two-step configuration Spring MVC Interceptor Interceptor A. Interceptor
;Public String Interceptor (actioninvocation Invocation) throws exception {
// Before calling the execute () method
String result = invocation. Invoke ();
// After the execute () method is called
Return result;
}
Note:
(1) When using a custom interceptor, the default built-in interceptor is lost. Therefore, you must explicitly reference the default
using interceptors
In web development we often encounter paging operations, a project may have more than one use to paging, then if the Java background using MyBatis as a persistence layer, we can use the MyBatis interceptor function to complete the entire project in multiple paging operations, reduce the code redundancy.
Interceptor Code
Intercept the Prepare m
interceptor is based on the Java reflection mechanism, and the filter is based on function callback.2. The filter depends on the servlet container, and the interceptor does not rely on the servlet container.3. the interceptor can only work on Action requests, while the filter can work on almost all requests.4. the
The Java Web Project uses the structure of the spring+spring mvc+hibernate, and the methods in the controller are used to process the front-end access information. The controller returns a Modelandview object to the front end or only JSON-formatted data by invoking the service for business processing. It is convenient for development and debugging if you can get information about HTTP requests that are processed in the backend program.
proxy object that makes different processing based on the customer's needs. For the customer, just know a proxy object on the line. In that Struts2, how is the interceptor invoked through a dynamic proxy? When the action request arrives, an action proxy object is generated by the agent of the system, which invokes the action's execute () or the specified method, and finds the interceptor corresponding to t
) and Interceptor (Interceptor), so Google. Boven Introduction:
1, interceptors are based on the reflection mechanism of Java, and filter is based on function callback.2. The filter relies on the servlet container, while the interceptor is not dependent on the servlet container.3. Interceptors only work on action reque
One, overview This article is mainly about how to customize the interceptor and through the custom interceptor to understand the interceptor lifecycle. Two, custom Interceptor 1) write an Interceptor class Hellointerceptor, which implements the
divide and conquer, so that each part of the logic can be highly reusable and highly scalable. Therefore, interceptor structure is really the essence of the struts2/xwork design of the pen. Definition of interceptor Execution Analysis interceptor
Let's take a look at the definition of Interceptor interface:
We have introduced some interceptor configurations and basic usage methods. So this time we will introduce the practical functions of the interceptor.
The utility interceptor completes permission control.
When a visitor needs to perform an operation, the application must first check whether the visitor is logged on and whether there are sufficient permissions
request ends , my interceptor gets the session data from the request, verifies that the user is logged in, if flag is true, returns False;2.posthandle (Request,response,handle, Modleandview): Explained by the Prehandle method we know that this method, including the Aftercompletion method that is to be mentioned later, can only be called if the return value of the Prehandle method of the current owning interceptor
Today, I spoke to several Java-loving students about struts2 and focused on its most valuable interceptor.
I don't know whether you can remember it. In struts2 server-side verification, I said, "The verification has been completed before arriving at the login action ". I hope someone can raise the question, so that I can say that this is the benefit of the interceptor
The Struts2 Interceptor (Interceptor) is a very important concept, and most of the functions in STRUTS2 are implemented through interceptors. Here's how to implement the Interceptor in Struts2 and try to customize an interceptor.
principle of Interceptor in 1 Struts2
Before
By default, if we define an interceptor for an action, the interceptor intercepts all methods in the action. However, in some cases, we do not want to intercept all methods. We only need to intercept some specific methods. In this case, we need to use the method filter feature of the struts2 interceptor.
To implement method filtering, struts2 providesMethodfilter
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.