拦截器为什么对main.jsp页面不进行拦截呢?
package com.imooc.restful.controller; import com.imooc.restful.entity.User; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.servlet.ModelAndView; import javax.servlet.http.HttpSession; @Controller public class UserController { @PostMapping("/login") public ModelAndView login(User user , HttpSession session){ ModelAndView mav = null; System.out.println(user); if (user.getName().equals("admin") && user.getPassword().equals("admin")){ mav = new ModelAndView("/main.jsp"); session.setAttribute("user" , user); System.out.println("user已保存至session,user=" + (User)session.getAttribute("user")); mav.addObject("u" , user); return mav; }else { mav = new ModelAndView("/login.jsp"); mav.addObject("msg" , "用户名或密码错误,请重新登陆"); return mav; } } }
package com.imooc.restful.inteceptor; import com.imooc.restful.entity.User; import org.springframework.web.servlet.HandlerInterceptor; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class LoginInterceptor implements HandlerInterceptor { @Override public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { User user = (User) request.getSession().getAttribute("user"); System.out.println("拦截器启动,user=" + user); if (user != null){ System.out.println("拦截器放行"); return true; }else { System.out.println("拦截器拦截"); response.sendRedirect("/login.jsp"); return false; } } }
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:context="http://www.springframework.org/schema/context" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mv="http://www.springframework.org/schema/mvc" xsi:schemaLocation="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"> <context:component-scan base-package="com.imooc.restful"/> <mvc:default-servlet-handler/> <mvc:annotation-driven/> <mvc:interceptors> <mvc:interceptor> <mvc:mapping path="/**"/> <mvc:exclude-mapping path="/login.jsp"/> <mvc:exclude-mapping path="/login**"/> <bean class="com.imooc.restful.inteceptor.LoginInterceptor"></bean> </mvc:interceptor> </mvc:interceptors> </beans>
22
收起
正在回答 回答被采纳积分+1
1回答
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星