拦截器可以拦截html却无法拦截jsp
老师,在做项目作业的时候,做了个拦截器,要求没正常登陆不可以访问后台页面,我做了个配置了个拦截器,可以正常拦截html页面,但是由于在html页面无法使用jstl标签库,我将其改成jsp页面,就是把html页面原本的主题部分复制到jsp内部的html部分全覆盖。但是,我发现在同目录下的这两个页面,原html页面会被正常拦截,但是后面创建并复制内容进去的jsp页面却无法被拦截,为什么?
dept_list.html:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 | <!DOCTYPE html> < html > < head > < meta charset = "UTF-8" > < title >科室列表</ title > < link rel = "stylesheet" href = "css/index.css" > < link rel = "stylesheet" href = "css/bootstrap.min.css" > </ head > < body > < header > < div class = "container" > < nav > < a href = "" >内科</ a > </ nav > < nav > < a href = "" >外科</ a > </ nav > < nav > < a href = "/category/list.do" >分类</ a > </ nav > </ div > </ header > < section class = "banner" > < div class = "container" > < div > < h1 >科室</ h1 > < p >科室列表</ p > </ div > </ div > </ section > < section class = "main" > < div class = "container" > < table class = "table table-striped" > < thead > < tr > < th >名称</ th > < th >分类</ th > < th >创建时间</ th > < th >最后修改时间</ th > </ tr > </ thead > < tbody > < c:forEach items = "${depts}" var = "dept" > < tr > < td >呼吸内科</ td > < td >1</ td > < td ></ td > < td ></ td > < td > < a href = "" >修改</ a > < a href = "" >删除</ a > </ td > </ tr > </ c:forEach > </ tbody > </ table > </ div > </ section > < section class = "page" > < div class = "container" > < div id = "fatie" > < a href = "/dept/addPrompt.do" > < button >新建</ button > </ a > </ div > </ div > </ section > < footer > copy@慕课网 </ footer > </ body > </ html > |
dept_list.jsp:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 | <%@ page contentType="text/html;charset=UTF-8" language="java" %> <%--<!DOCTYPE html>--%> < html > < head > < meta charset = "UTF-8" > < title >科室列表</ title > < link rel = "stylesheet" href = "css/index.css" > < link rel = "stylesheet" href = "css/bootstrap.min.css" > </ head > < body > < header > < div class = "container" > < nav > < a href = "" >内科</ a > </ nav > < nav > < a href = "" >外科</ a > </ nav > < nav > < a href = "/category/list.do" >分类</ a > </ nav > </ div > </ header > < section class = "banner" > < div class = "container" > < div > < h1 >科室</ h1 > < p >科室列表</ p > </ div > </ div > </ section > < section class = "main" > < div class = "container" > < table class = "table table-striped" > < thead > < tr > < th >名称</ th > < th >分类</ th > < th >创建时间</ th > < th >最后修改时间</ th > </ tr > </ thead > < tbody > < c:forEach items = "${depts}" var = "dept" > < tr > < td >呼吸内科</ td > < td >1</ td > < td ></ td > < td ></ td > < td > < a href = "" >修改</ a > < a href = "" >删除</ a > </ td > </ tr > </ c:forEach > </ tbody > </ table > </ div > </ section > < section class = "page" > < div class = "container" > < div id = "fatie" > < a href = "/dept/addPrompt.do" > < button >新建</ button > </ a > </ div > </ div > </ section > < footer > copy@慕课网 </ footer > </ body > </ html > |
拦截器配置:
1 2 3 4 5 6 7 8 | <!--登录拦截器配置--> < mvc:interceptors > < mvc:interceptor > <!--拦截所有路径--> < mvc:mapping path = "/**" /> < bean class = "com.lgq.hospital.global.LoginInterceptor" /> </ mvc:interceptor > </ mvc:interceptors > |
拦截器LoginInterceptor.java:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 | package com.lgq.hospital.global; import org.springframework.web.servlet.HandlerInterceptor; import org.springframework.web.servlet.ModelAndView; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; public class LoginInterceptor implements HandlerInterceptor { //preHandle方法,运行controller方法之前执行此方法 public boolean preHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o) throws Exception { System.out.println( "登录拦截器的preHandle方法执行了" ); String url = httpServletRequest.getRequestURI(); if (url.toLowerCase().indexOf( "login" ) >= 0 ) { //检查路径是否存在“login”,存在的话则直接放行 return true ; } HttpSession session = httpServletRequest.getSession(); if (session.getAttribute( "user" ) != null ) { //检查会话中的user变量是否为空,不为空的话则直接放行 return true ; } httpServletResponse.sendRedirect( "login.html" ); return false ; } //postHandle方法,在controller方法得到结果以后(return之后),但响应还没有真正返回的时候执行(即渲染视图之前) public void postHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, ModelAndView modelAndView) throws Exception { System.out.println( "登录拦截器的postHandle方法执行了" ); } //afterCompletion,请求完全处理完后执行 public void afterCompletion(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) throws Exception { System.out.println( "登录拦截器的afterCompletion方法执行了" ); } } |
(1)启动项目后,在浏览器地址栏输入http://localhost:8080/hospital_web/dept_list.html
老师解释一下这里,虽然是被正常拦截了,但是地址栏上的url为什么显示的确实另一个页面地址,这里的hospital_detail是前台详情页面的url地址。之后我再次访问dept_list.html被拦截后,地址栏显示的就是正常的,是登录页面的地址login.html
(2)启动项目后,在浏览器地址栏输入http://localhost:8080/hospital_web/dept_list.jsp
正在回答
同学你好。
1、回车后访问地址改变的问题,有可能是idea设置的默认访问页导致的,例如下图的URL。在同学敲击回车时,可能还没有完全加载,然后在被拦截后,却被重定向到hospital_detail
2、编码过滤器不访问的问题,springmvc的拦截器就是不拦截jsp的,但是可以拦截对controller的请求,所以,可以将其放到web-inf下,通过转发访问,否则就只能用过滤器去拦截。
如果解答了同学的疑问,望采纳~
祝学习愉快~
- 参与学习 人
- 提交作业 323 份
- 解答问题 8263 个
本阶段将带你学习主流框架SSM,以及SpringBoot ,打通成为Java工程师的最后一公里!
了解课程
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧