PackageCom.sxt.interceptor;Importjavax.servlet.http.HttpServletRequest;ImportJavax.servlet.http.HttpServletResponse;ImportOrg.springframework.web.servlet.HandlerInterceptor;ImportOrg.springframework.web.servlet.ModelAndView; Public classMyinterceptorImplementsHandlerinterceptor { Public voidAftercompletion (HttpServletRequest request, httpservletresponse response, Object handler, Exception ex)throwsException {System.out.println ("Last Execution!!! Generally used to release resources!! "); } Public voidPosthandle (httpservletrequest request,httpservletresponse response, Object handler, Modelandview Modelandview)throwsException {System.out.println ("After action executes, the view is executed before the build!! "); } Public BooleanPrehandle (httpservletrequest request,httpservletresponse response, Object handler)throwsException {System.out.println ("Action before executing!!! "); return true;//continue with Action }}
Myinterceptor
PackageCom.sxt.interceptor;Importjavax.servlet.http.HttpServletRequest;ImportJavax.servlet.http.HttpServletResponse;ImportOrg.springframework.web.servlet.handler.HandlerInterceptorAdapter; Public classMyInterceptor2extendsHandlerinterceptoradapter {@Override Public BooleanPrehandle (httpservletrequest request,httpservletresponse response, Object handler)throwsException {System.out.println ("Myinterceptor2.prehandle ()"); return true;//continue with Action }}
MyInterceptor2
PackageCom.zhouwuji.controller;Importjavax.servlet.http.HttpServletRequest;ImportJavax.servlet.http.HttpServletResponse;ImportOrg.springframework.stereotype.Controller;ImportOrg.springframework.ui.ModelMap;Importorg.springframework.web.bind.annotation.PathVariable;Importorg.springframework.web.bind.annotation.RequestMapping;ImportOrg.springframework.web.servlet.ModelAndView; @Controller Public classLogincontroller {/*@RequestMapping ("/login.do") public Modelandview Login (String name,httpservletrequest request, HttpServletResponse response) {Modelandview mv=new modelandview (); Mv.addobject ("ad", name); Mv.setviewname ("index"); return MV; }*/@RequestMapping ("/login.do") Publicstring Login (String Name,modelmap modelmap) {System.out.println (name); Modelmap.addattribute ("Name", name); return"Index"; } //Delete? id=123 RESTful style Requesthttp://localhost: 8080/project/abc/1/delete.do//@PathVariable int String can be a@RequestMapping ("/delete/{id}") Publicstring Hello (@PathVariable string id,modelmap modelmap) {System.out.println (ID); Modelmap.addattribute ("Name", id); return"Index"; } //@PathVariable int String multiple@RequestMapping ("/{id}/{uid}/delete") PublicString Hello1 (@PathVariable string ID, @PathVariableintUid,modelmap Modelmap) {System.out.println (ID); SYSTEM.OUT.PRINTLN (UID); Modelmap.addattribute ("Name", id); return"Index"; }/*action before executing!!! Myinterceptor2.prehandle () Sdfaction executes before generating the view!! Last Execution!!! Generally used to release resources! */ }
Logincontroller
<%@ page language= "java" contenttype= "text/html; Charset=utf-8 " pageencoding=" UTF-8 "%><%@ taglib uri=" http://java.sun.com/jsp/jstl/ Core "prefix=" C "%><! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" "Http://www.w3.org/TR/html4/loose.dtd" > value: ${name} </body>
index.jsp<?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:context= "Http://www.springframework.org/schema/context"Xmlns:mvc= "Http://www.springframework.org/schema/mvc"xsi:schemalocation= "Http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd http://Www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsdhttp//Www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context-4.1.xsd"><beanclass= "Org.springframework.web.servlet.view.InternalResourceViewResolver" > <property name= "prefix" value= "/ web-inf/view/"/> <property name=" suffix "value=". jsp "/> </bean> <context:component-scan B ase- Package= "Com.zhouwuji.controller" ></context:component-scan> <mvc:interceptors> <beanclass= "Com.sxt.interceptor.MyInterceptor" ></bean> <!--intercept all SPRINGMVC url! -<mvc:interceptor> <mvc:mapping path= "/login.do"/> <!--<mvc:mapping Path= "/test/*"/>--> <beanclass= "Com.sxt.interceptor.MyInterceptor2" ></bean> </mvc:interceptor> </mvc:interceptors> < /beans>
Springmvc-servlet.xml<?xml version= "1.0" encoding= "UTF-8"? ><web-app xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance" xmlns= "Http://java.sun.com/xml/ns/javaee" xsi:schemalocation= "Http://java.sun.com/xml/ns/javaee/http Java.sun.com/xml/ns/javaee/web-app_2_5.xsd "id=" webapp_id "version=" 2.5 "> <filter> <filter-name>ch Aracterencodingfilter</filter-name> <filter-class>org.springframework.web.filter.characterencodingfilter</filter-class> <init-param> <param-name>encoding</param-name> <param-value>utf- 8</param-value> </init-param> <init-param> <param-name>forcerequestencodi Ng</param-name> <param-value>true</param-value> </init-param> <init-param> <param-name>forceresponseencodi Ng</param-name> <param-value>true</param-value> </init-param> </filter> <filter-mapping> <filter-name>cha Racterencodingfilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <servlet> <servlet-name>springmvc</servlet- name> <servlet-class> Org.springframework.web.servlet.DispatcherServlet </servlet-cla ss> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <s Ervlet-name>springmvc</servlet-name> <url-pattern>*.do</url-pattern> </SERVLET-MAPPING&G T <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list></we B-app>
Web. XML<%@ page language= "java" contenttype= "text/html; Charset=utf-8 " pageencoding=" UTF-8 "%><! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" "Http://www.w3.org/TR/html4/loose.dtd" > name : <input type= "text" name= "name" > <input type= "Submit" value= "Submit" > </form > </fieldset> <a href= "delete/abc.do" > RESTful style Request one of the parameters </a> <a href= "abc/1/delete.do" > RESTful style request multiple parameters </a></body>
index.jsp<project xmlns= "http://maven.apache.org/POM/4.0.0" xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"xsi:schemalocation= "http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" > <modelversion>4.0.0 </modelVersion> <groupId>com.zhouwuji.wuji</groupId> <artifactid>project</artifactid > <version>0.0.1-SNAPSHOT</version> <packaging>war</packaging> <dependencies> <!--https://mvnrepository.com/artifact/commons-logging/commons-logging --<dependency> <groupId>commons-logging</groupId> <artifactid>commons-logging& Lt;/artifactid> <version>1.1.1</version> </dependency> <!--https://MVNREPOSITORY.COM/ARTIFACT/ORG.SPRINGFRAMEWORK/SPRING-WEBMVC --<dependency> <groupId>org.springframework</groupId> <ARTIFACTID>SPRING-WEBMV C</artifactid> <version>4.1.6.RELEASE</version> </dependency> <!--htt Ps://Mvnrepository.com/artifact/org.springframework/spring-context-support --<dependency> <groupId>org.springframework</groupId> <artifactid>spring-conte xt-support</artifactid> <version>4.1.6.RELEASE</version> </dependency> &L t;! --HTTPS://Mvnrepository.com/artifact/jstl/jstl --<dependency> <groupId>jstl</groupId> <artifactId>jstl</artifactId> <version>1.2</version> </dependency> <dependency> <groupId> Commons-io</groupid> <artifactId>commons-io</artifactId> <version>2.4</ve rsion> </dependency> <dependency> <groupid>commons-fileupload</groupid> ; <artifactId>commons-fileupload</artifactId> <version>1.2.2</version> </depend Ency> </dependencies></project>
Pom.xmlJava Framework--springmvc--intercepter-restfull + maven