Spring MVC-The implementation of interceptors and examples of user landings
1. Intercepting device
The interceptor in SPRINGMVC implements the Handlerinterceptor interface, commonly used with identity authentication, authorization and checksum, template view, unified processing, etc.
The public class HanderInterceptor1 implements Handlerinterceptor {@Override the public
void Aftercompletion ( HttpServletRequest arg0,
httpservletresponse arg1, Object arg2, Exception arg3)
throws Exception {
}
@ Override public
void Posthandle (HttpServletRequest arg0, HttpServletResponse arg1,
Object arg2, Modelandview ARG3) throws Exception {
}
@Override public
boolean prehandle (HttpServletRequest arg0, HttpServletResponse arg1,
Object arg2) throws Exception {return
true;
}
}
There are three methods in the Interceptor:
Prehandler: Before entering the handler method, the use of identity authentication, identity authorization, login verification, such as identity authentication, the user did not login, interception is no longer down, the return value is False, you can intercept; otherwise, when True is returned, interception is not performed;
Posthandler: After entering the handler method, return to Modelandview before the execution, using the scene from the Modelandview parameters, for example, the common model data passed in here to the view, you can also specify the view of the display;
Afterhandler: Implement this method after the execution of handler, so that it can be used for unified exception handling, unified log processing, etc.
2. The Interceptor Configuration
The interceptor configuration is implemented in two ways:
(1) for a handlermapping (Controller) configuration
SPRINGMVC Interceptor for a controller to intercept settings, if the interception in a handlermapping, after the handlermapping map successful handler eventually use the interceptor;
(2) similar to global configuration
A similar global interceptor can be configured, and the SPRINGMVC framework injects a configuration-like global interceptor into each handlermapping;
Configuration implementation:
<!--Configure interceptors-->
<mvc:interceptors>
<!--multiple interceptors, sequentially execute-->
<mvc:interceptor>
<!--/** represents all URLs, including the child URL path-->
<mvc:mapping path= "/**"/> <bean "class=
" Cn.labelnet.ssm.filter.HanderInterceptor1 "></bean>
</mvc:interceptor>
<!--configuration Landing Interceptor >
<mvc:interceptor>
<mvc:mapping path= "/**"/>
<bean class= " Cn.labelnet.ssm.filter.LoginHandlerIntercepter "></bean>
</mvc:interceptor>
<!--
.....
-->
</mvc:interceptors>
(3) In a project, you can configure multiple interceptors, using multiple interceptors, you should note:
When multiple interceptors are in use, the Prehandler are sequentially executed, while the Posthandler and Afterhandler are executed in reverse order;
so:
If the Unified log processor interceptor, Need to change interceptor prehandler be sure to return true, and place it in the first position of the interceptor configuration;
if you log in to the authentication interceptor, place it in the first position in the Interceptor's configuration (which is logged, under log processing), and
if you have permission to verify the Interceptor, is placed in the landing interceptor, because the landing after the pass, before you can verify the authority;
3. Example:
Scene Description: When the user clicks the view, we carry on the landing interceptor operation, determines whether the user logs in. Landing, do not intercept, did not log on, then go to the landing interface;
icon:
3.1 Controller landing Business implementation
@RequestMapping ("/clientlogin") public
String clientlogin (HttpSession httpsession,string username,string Password) {
if (username.equals ("Yuan") &&password.equals ("123456")) {
//Login Successful
Httpsession.setattribute ("username", username);
return "Forward:clientsList.action";
} else{
//Login failed return
"forward:login.jsp";
}
3.2 Controller Logout Business implementation
@RequestMapping ("/clientloginout") public
String clientloginout (HttpSession HttpSession) {
Httpsession.invalidate ();
return "Forward:clientsList.action";
}
3.3 Interceptor Implementation
The implementation of user interception here is: by judging whether the page is edited to view, if it is, to determine the user name in the session does not exist, it can be;
Package cn.labelnet.ssm.filter;
Import Javax.servlet.http.HttpServletRequest;
Import Javax.servlet.http.HttpServletResponse;
Import javax.servlet.http.HttpSession;
Import Org.springframework.web.servlet.HandlerInterceptor;
Import Org.springframework.web.servlet.ModelAndView;
/** * Landing Interceptor * Scene: The user clicks to view, we carry on the landing interceptor operation, judge whether the user landed. * Landing, do not intercept, did not log on, then go to the landing interface; * TODO * Author: Original Zhuo * Time: January 8, 2016 3:25:35 * Project: Springmvcmybatis1demo * /public class Loginhandlerintercepter implements Handlerinterceptor {@Override public void AF
Tercompletion (HttpServletRequest request, httpservletresponse response, Object arg2, Exception arg3) Throws Exception {} @Override public void Posthandle (HttpServletRequest arg 0, HttpServletResponse arg1, Object arg2, Modelandview arg3) throws Exception {} @ OverRide public boolean prehandle (HttpServletRequest request, HttpServletResponse arg1, Object Arg
2) throws Exception {String RequestUri = Request.getrequesturi (); if (Requesturi.indexof ("Editclientifo.action") >0) {//description is in the edited page HttpSession
Session = Request.getsession ();
String username = (string) session.getattribute ("username");
if (username!=null) {//Login successful user return true; }else{//No landing, turn to landing interface Request.getrequestdispatcher ("/login.jsp"). Forwa
RD (REQUEST,ARG1);
return false;
}}else{return true;
}
}
}
3.4 Interceptor Configuration Implementation
<!--Configure interceptors-->
<mvc:interceptors>
<!--multiple interceptors, sequentially execute-->
<mvc:interceptor>
<!--/** represents all URLs, including the child URL path-->
<mvc:mapping path= "/**"/> <bean "class=
" Cn.labelnet.ssm.filter.HanderInterceptor1 "></bean>
</mvc:interceptor>
<!--configuration Landing Interceptor >
<mvc:interceptor>
<mvc:mapping path= "/**"/>
<bean class= " Cn.labelnet.ssm.filter.LoginHandlerIntercepter "></bean>
</mvc:interceptor>
<!--
.....
-->
</mvc:interceptors>
3.5 Landing Page Implementation
<%@ page language= "java" import= "java.util.*" pageencoding= "Utf-8"%> <% String Path = Request.get
ContextPath ();
String basepath = request.getscheme () + "://" +request.getservername () + ":" +request.getserverport () +path+ "/"; %> <! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" >
3.6 List Information page
<body>
4.Demo Free Integral download
http://download.csdn.net/detail/lablenet/9396235
Original connection: thank the author.
http://blog.csdn.net/LABLENET/article/details/50483674