Continue to learn the use of the Interceptor in spring MVC in the front, following an example (the full item is downloaded here: http://download.csdn.net/detail/u012116457/8433425).
First, add Interceptor-servlet.xml to the project to configure the interceptor, and of course, the configuration must be loaded at Tomcat startup in Web. XML, as follows:
<!--1. Configure the Spring distributor (the URL that is blocked by the total control center will converge to the servlet)--><servlet> <SERVLET-NAME>SPRINGMVC </servlet-name> <servlet-class>org.springframework.web.servlet.dispatcherservlet</ Servlet-class> <!--Configure Spring context (load spring MVC configuration files from this directory)-- <init-param> < Param-name>contextconfiglocation</param-name> <param-value>/web-inf/classes/spring/* servlet.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> <!--tomcat loads--></servlet> immediately after booting
The main function of this interceptor is when the page requests to log in, if the time range allowed to log in, then normal execution, otherwise jump to a specific page.
Interceptor-servlet.xml as follows:
<?xml version= "1.0" encoding= "UTF-8"? ><beans xmlns= "Http://www.springframework.org/schema/beans" xmlns: Xsi= "Http://www.w3.org/2001/XMLSchema-instance" xmlns:p= "http://www.springframework.org/schema/p" xmlns:mvc= " Http://www.springframework.org/schema/mvc "xmlns:context=" Http://www.springframework.org/schema/context "xmlns: Util= "Http://www.springframework.org/schema/util" xsi:schemalocation= "http://www.springframework.org/schema/ Beans Http://www.springframework.org/schema/beans/spring-beans-3.0.xsd Http://www.springframework.org/schem A/context http://www.springframework.org/schema/context/spring-context-3.0.xsd Http://www.springframework . ORG/SCHEMA/MVC http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd Http://www.springframewo Rk.org/schema/util http://www.springframework.org/schema/util/spring-util-3.0.xsd "> <!--Interceptor--><mvc: Interceptors> <!--multiple interceptors, sequential execution--<!--Interceptor 1 landing blocker--<mvc:interceptor> <mvc:mapping path= "/login/login.do"/><!--can write multiple, if/*, will intercept all controllers---&L T;bean class= "Module.interceptor.LoginInterceptor" > <!--starttimestr property specifies the start time to allow login-- <property name= "Starttimestr" > <value>07:00:00</value> </prope Rty> <!--Endtimestr property specifies the end time to allow login--<property name= "Endtimestr" > <value>23:00:00</value> </property> <!--outtimepageur The url--> <property name= "Outtimepageurl" > <value>h when the L property specifies that the page is not in the time range ttp://localhost:8080/springmvc/index.jsp </value> </property> &L T;/bean> </mvc:interceptor></mvc:interceptors></beans>
Request is intercepted after processing class:
Package Module.interceptor;import Java.text.simpledateformat;import Java.util.date;import Javax.servlet.http.httpservletrequest;import Javax.servlet.http.httpservletresponse;import Org.springframework.web.servlet.handlerinterceptor;import org.springframework.web.servlet.modelandview;/** * Login Operation Interceptor * * @author Administrator * */public class Logininterceptor implements Handlerinterceptor {private String start Timestr;private string Endtimestr;private string outtimepageurl; @Overridepublic void Aftercompletion ( HttpServletRequest arg0,httpservletresponse arg1, Object arg2, Exception arg3) throws Exception {//TODO auto-generated Me Thod stub} @Overridepublic void Posthandle (HttpServletRequest arg0, HttpServletResponse arg1,object arg2, Modelandview ARG3) throws Exception {//TODO auto-generated Method stub} @Overridepublic boolean Prehandle (HttpServletRequest request, HttpServletResponse response,object arg2) throws Exception {SimpleDateFormat hms=new simpledateformat ("HH:mm:ss");D Ate StarttimEdat=hms.parse (STARTTIMESTR);D ate endtimedat=hms.parse (ENDTIMESTR);D ate now = new Date (); String nowhms = Hms.format (now); Now=hms.parse (NOWHMS); if (Starttimedat.before (now) &&now.before (Endtimedat)) {//current time within the specified time frame System.out.println ("Allow login at current time"); return true;} Else{system.out.println ("Current time not Allowed"); Response.sendredirect (outtimepageurl);//Return to the prompt page return false;}} The Get and Set methods public String Getstarttimestr () {return starttimestr;} public void Setstarttimestr (String starttimestr) {this.starttimestr = Starttimestr;} Public String Getendtimestr () {return endtimestr;} public void Setendtimestr (String endtimestr) {this.endtimestr = Endtimestr;} Public String Getouttimepageurl () {return outtimepageurl;} public void Setouttimepageurl (String outtimepageurl) {this.outtimepageurl = Outtimepageurl;}}
The control class for landing page is as follows:
Package Module.controller;import Javax.annotation.resource;import Module.entity.user;import Module.service.loginservice;import Org.springframework.stereotype.controller;import Org.springframework.web.bind.annotation.requestmapping;import Org.springframework.web.bind.annotation.RequestMethod; @Controller @requestmapping ("/login") public class Logincontroller {private static string INDEX = "Module/jsp/index";p rivate static string LOGIN = "Module/jsp/login";//@Aut Owired () @Qualifier ("LoginServiceImpl2") @Resource (name= "LOGINSERVICEIMPL2") Private Loginservice Loginservice ; @RequestMapping (value = "/login.do", method = requestmethod.post) public String login (user user) {System.out.println ( User.getname ()); User u = loginservice.findbyname (User.getname ()), if (U = null) System.out.println (U.getpassword ()), if ("admin". Equals (User.getname ())) return Index;return LOGIN;}}
When the client sends a request: Http://localhost:8080/springMVC/login/login.do, the request will be intercepted, submitted to logininterceptor for processing, return True to execute the login logic, otherwise jump to the prompt page.
Play Spring MVC (vii)----interceptor