Configuration and use of SPRINGMVC interceptors:
1. Customize the interceptor to implement the Handlerinterceptor interface.
Package Com.bybo.aca.web.interceptor;import Javax.servlet.http.httpservletrequest;import Javax.servlet.http.httpservletresponse;import Org.springframework.web.servlet.handlerinterceptor;import Org.springframework.web.servlet.modelandview;public class Login implements Handlerinterceptor {@Override public voi D aftercompletion (HttpServletRequest HttpRequest, HttpServletResponse HttpResponse, Object arg2, Exception arg3 ) throws Exception {} @Override public void Posthandle (HttpServletRequest arg0, HTTPSERVLETR Esponse arg1, Object arg2, Modelandview arg3) throws Exception {} @Override public boolean PR Ehandle (HttpServletRequest request, HttpServletResponse response, Object object) throws Exception {/*ht Tpservletrequest HttpRequest = (httpservletrequest) request; HttpServletResponse HttpResponse = (httpservletresponse) response;*/String urlstring = Request.getrequesturi (); Olforum/forumlist.html Impersonation Login Page if (Urlstring.endswith ("forumlist.html")) {return true; }//path requested by String Contextpath=request.getcontextpath (); /*httprequest.getrequestdispatcher ("/olforum/forumlist"). Forward (HttpRequest, httpresponse); */* Response.sendredirect (contextpath+ "/olforum/forumlist.html"); */Response.sendredirect (ContextPath + "/olForum/ Forumlist.html?login=aaa "); return false; /*httpresponse.sendredirect (Httprequest.getcontextpath () + "/olforum/forumlist.html"); return;*/}}
Configuration in 2:SPRINGMVC configuration file:
<?xml version= "1.0" encoding= "UTF-8"? ><beans xmlns= "Http://www.springframework.org/schema/beans" xmlns: aop= "HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP" xmlns:context= "Http://www.springframework.org/schema/context" Xmlns:mvc= "Http://www.springframework.org/schema/mvc" xmlns:tx= "Http://www.springframework.org/schema/tx" xmln S:xsi= "Http://www.w3.org/2001/XMLSchema-instance" xmlns:p= "http://www.springframework.org/schema/p" Xsi:schema location= "Http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd Http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd/HTTP Www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd/HTTP WWW.SPRINGFRAMEWORK.ORG/SCHEMA/MVC http://www.springframework.org/schema/mvc/spring-mvc.xsd/HTTP Www.springframework.org/schema/tx Http://www.springfraMework.org/schema/tx/spring-tx.xsd "> <!--Scan Controller (Controller layer injection)--<context:component-scan base- Package= "Com.bybo.aca.web.controller"/> <mvc:interceptors> <!--use beans to define a interceptor, directly defined in MVC: The interceptor below the interceptors root will intercept all requests-<!--<bean class= "Com.bybo.aca.web.interceptor.Login"/> <mvc:interceptor> <!--interception:/** to intercept all controllers--<mvc:mapping path= "/**"/>
<!--do not intercept--<mvc:exclude-mapping path= "/index.html"/> <bean class= "com.b Ybo.aca.web.interceptor.Login "/> </mvc:interceptor> </mvc:interceptors>
<bean id= "Viewresolver" class= "Org.springframework.web.servlet.view.InternalResourceViewResolver" P:PR efix= "/web-inf/views/" p:suffix= ". jsp"/> </beans>
SPRINGMVC Login Blocker