The user-friendly front-end SpringMVC & Struts2 interceptor Detailed

Directly to the theme of it!
First, the configuration Struts2 interceptor two steps
a configuration corresponding to the interceptor classes:
2 interceptor arranged in the configuration file Struts.xml
configure interceptor class has three methods in Strust2
an implement Interceptor
2 Inheritance AbstractInterceptor
. 3 by inheritance MethodFilterInterceptor class
: the difference between
whether a filtration support:
the use of the first and second kinds fact, almost, Action will intercept all the way, but the first three kinds of not the same, because the first three kinds of support filtering method, you can specify the way we want to block, (using includeMethods need to intercept specified methods - using excludeMethods indicate the method does not require the interceptor to intercept, so we usually use the first three kinds configure struts2 interceptor
3 through inheritance MethodFilterInterceptor class, rewrite doIntercept method
------- major role in this example: check whether the user is logged in, (the session by determining whether the user name is empty) when the user logs in the management window, if not jump to the login login interface.

RockStar class the extends MethodFilterInterceptor {public
// rewrite doInterceptor to achieve our interceptors are configured ----- Action Invocation Action caller, the example of the state of implementation of an action on behalf of, holders of examples of interceptors and the action to be performed .
String doIntercept protected (the ActionInvocation Invocation) throws Exception {
// get ActionCotext instance (the ActionContext contains the Session)
the ActionContext ActionContext ActionContext.getContext = ();
// get the corresponding Session, ActionContext actually map mapping a large structure, key mapped value
the map <String, Object> = actionContext.getSession session ();
// get the corresponding attribute of the session (the attribute is set to the class object, cast)
String username = (String) Session.get ( "username ");
// determines whether corresponding data exists in the session to know whether the user is logged
iF (! = null && sername.equals username (" CCHH ")
{
// clearance exists
System.out.println (" legitimate user .... "+ username);
// next process is a notification of this action is then Struts2 buckle down of the dry matter, such as the next call to the next interceptor or the Action, corresponding to the current interceptor exit the
return invocation.invoke ();
}
the else {
System.out.println ( "the user is not signed ...");
// if the verification fails, a login string is returned, enter the login screen
return "login";
}
}
}
there are a lot of people, probably for the ActionContext very confused, very confused on the Map, and then run, you will understand, everything has its reason!
Key explain, ActionContext ---- action context,
simplified version:
Struts ---- According to request ----> create ActionContext {Session, parameter, etc.}, to map form, key / value store data objects --- --- by getContext ----> get the current ActionContext
detailed version:
1Strusts2 based request corresponds to each execution thread Http request to create the corresponding ActionContext
-------> that is a request has a unique thread ActionContext
-------> we can use the static method getContext ()
-------> ActionContext to get the current thread (the thread Sturts only to request the creation of a request to create ActionContext)

2 Meanwhile, the ActionContext saved some during Action execution of the required objects and data, such as Session, parameters, etc., and use the form Map collection, which is why we can obtain the corresponding session through the Action Context, turned out to be since it is stored in the context of the Action

Two interceptor configured in the configuration file Struts.xml the
steps of:
a registered interceptor class we knock
2 interceptor define a stack, and the corresponding interceptor arranged in the stack, and may be arranged in the corresponding Interceptor which method interceptor, which method does not intercept
3 we typically arranged <default-interceptor-ref name = " interceptor stack name" /> to automatically intercepted by the Action
. 4 can also <interceptor-ref name = "interceptor stack name "/>

<Package default name = "the extends =" Struts-default "namespace" / ">
<interceptors>
<-! Register interceptor. 1 ->
<Interceptor name =" Rockstar "class =" com.ali.demo.Rockstar " >
</ intertceptor>
<-! interceptor stack configuration ->
<interceptor stack-name = "myStack">
<-! executing the Struts blocked by default ->
<interceptor-REF name = "defaultStack"> < / interception-ref>
<-! attention to this place, to default interceptors placed at the front, so that will be loaded by default struts2 interceptors must be loaded ->
<interceptor-ref name = "Rockstar">
< ! - Specifies which method does not intercept, exclude, negative meaning ->
<param name = "excludeMethods"> Login </ param>
</ intercepor-REF>
</ interceptor-Stack>
</ interceptors>
// called automatically for all Action interceptor stack
// <default-interceptor-ref name = "myStack"> </ default-interceptor-ref>


------------------------ completes the registration and configuration of the interceptor
1 if the method is intercepted, the interceptor is returned directly from the not then continue.
2 If the method is not intercepted, it will then be performed down
while using the tag <interceptor-ref name = "myStack " /> to specify the interceptor stack to a corresponding class

<! - Access Management Action page back through the ->
<Action name = "the auth">
<Result> /WEB-INF/jsp/manage.jsp </ Result>
<! - the return path check failed, the corresponding the interceptor login string ->
<Result name = "login"> / the login.jsp </ Result>
<-! interceptor corresponding references in the action of the stack ->
<name = REF-interceptor " myStack "> </ Interceptor-REF>
</ Action>


<action name="manager" class="userAction" method={1}>
<result name="name">/WEB-INF/jsp/manage.jsp</result>
<result name="error">/login.jsp</result>
</action>
</package>

These are the Struts2 interceptor for the answer!

SpringMVC interceptor configuration
Next look at SpringMVC interceptor configuration, Struts2 will be much simpler than
first understand SpringMVC interceptors mainly by implementing HandlerInterceptor interface class and override its three methods, namely former preHandler (in after the request arrives before controller, intercept) in PostHandler (request reaches the controller, and the parameters are not returned before ModelandView) afterHandler (after completion of execution of the method)
is also in two steps:
1. write interceptor class:

{class SpMvc HandlerInterceptor from the implements public
** ** // request before reaching the Controller
public Boolean The preHandle (the HttpServletRequest Request, the HttpServletResponse reponse, Object obj) throws Exception {
** // get the Session, **
the HttpSession Request.getSession the session = ();
// ** ** acquires the corresponding values from the session
String name = (String) session.getAttribute ( "name");
** // this value determines whether there **
! iF (name = null && name.equals ( "CCHH ") {
System.out.println (" lawful user, by blocking ");
return true; // ** true where the representative via the interceptor **
}
the else
Response.sendRedirect (" index "); // jump pages the index page
return false; // ** ** intercepted herein represents false
}
// ** request reaches the Controller, and the parameter is not returned until ModelandView **
The postHandle void public (the HttpServletRequest Request, Response the HttpServletResponse, Object obj, ModelAndView MAV)
throws Exception {
System.out.println ( ".... In this case the method is performed");
}
** // method after the request is finished , without a corresponding object parameters of ModelAndView **
{public class afterHandle (the HttpServletRequest Request, Response HttpServletResponse, Object obj) throws Exception
( "after execution of the method ....") System.out.println;
}
}
2 in SpringMVC interceptor configuration corresponding
SpringMVC interceptor arranged with respect Struts2, many relatively simple
mainly
an interceptor arranged interceptor class
request is provided to block the 2 <mvc: mapping /> struts2 is <param name = " includeMethods "/>
. 3 is not set intercepted request <mvc: exclude-mapping /> struts2 is <param name =" excludeMethods ">

<! - Configuration interceptor ->
<MVC: interceptors>
: <MVC Interceptor>
<! - where classpath interceptor ->
<the bean class = "com.ali.demo.SpMvc" /> </ the bean>
! <- intercept a request, the intercepted request ->
<MVC: Mapping path = "/ **" />
! <- does not intercept the request ->
<MVC:-Mapping the exclude path = "/ LoginController / login.do "/>
<./ MVC: Interceptor>
</ MVC: interceptors>

Guess you like

Origin www.cnblogs.com/ly570/p/10961069.html