首页 文章资讯内容详情

springmvc拦截器的配置、使用

2026-06-01 4 花语

本文内容纲要:

springmvc拦截器的配置、使用:

1.自定义拦截器,实现HandlerInterceptor接口。 packagecom.bybo.aca.web.interceptor; importjavax.servlet.http.HttpServletRequest; importjavax.servlet.http.HttpServletResponse; importorg.springframework.web.servlet.HandlerInterceptor; importorg.springframework.web.servlet.ModelAndView; publicclassLoginimplementsHandlerInterceptor{ @Override publicvoidafterCompletion(HttpServletRequesthttpRequest, HttpServletResponsehttpResponse,Objectarg2,Exceptionarg3) throwsException{ } @Override publicvoidpostHandle(HttpServletRequestarg0,HttpServletResponsearg1, Objectarg2,ModelAndViewarg3)throwsException{ } @Override publicbooleanpreHandle(HttpServletRequestrequest,HttpServletResponseresponse, Objectobject)throwsException{ /*HttpServletRequesthttpRequest=(HttpServletRequest)request; HttpServletResponsehttpResponse=(HttpServletResponse)response;*/ StringurlString=request.getRequestURI(); ///olForum/forumList.html模拟登录页 if(urlString.endsWith("forumList.html")){ returntrue; } //请求的路径 StringcontextPath=request.getContextPath(); /*httpRequest.getRequestDispatcher("/olForum/forumList").forward(httpRequest,httpResponse);*/ /*response.sendRedirect(contextPath+"/olForum/forumList.html");*/ response.sendRedirect(contextPath+"/olForum/forumList.html?login=aaa"); returnfalse; /*httpResponse.sendRedirect(httpRequest.getContextPath()+"/olForum/forumList.html"); return;*/ } }

2:springmvc配置文件中配置:

<?xmlversion="1.0"encoding="UTF-8"?> <beansxmlns="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" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" xsi:schemaLocation="http://www.springframework.org/schema/aophttp://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvchttp://www.springframework.org/schema/mvc/spring-mvc.xsd http://www.springframework.org/schema/txhttp://www.springframework.org/schema/tx/spring-tx.xsd"> <!--扫描controller(controller层注入)--> <context:component-scanbase-package="com.bybo.aca.web.controller"/> <mvc:interceptors> <!--使用bean定义一个Interceptor,直接定义在mvc:interceptors根下面的Interceptor将拦截所有的请求--> <!--<beanclass="com.bybo.aca.web.interceptor.Login"/>--> <mvc:interceptor> <!--进行拦截:/**表示拦截所有controller--> <mvc:mappingpath="/**"/> <!--不进行拦截--> <mvc:exclude-mappingpath="/index.html"/> <beanclass="com.bybo.aca.web.interceptor.Login"/> </mvc:interceptor> </mvc:interceptors> <beanid="viewResolver"class="org.springframework.web.servlet.view.InternalResourceViewResolver" p:prefix="/WEB-INF/views/"p:suffix=".jsp"/> </beans>

本文内容总结:

原文链接:https://www.cnblogs.com/super-chao/p/6496428.html