这是我实践Spring项目。我无法重定向到SpringMVC Interceptor中的正确页面。
spring-mvc.xml
<?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/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">
<description>Spring MVC Configuration</description>
<context:property-placeholder ignore-unresolvable="true" location="classpath:myshop.properties"/>
<context:component-scan base-package="com.huahua.my.shop" use-default-filters="false">
<context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
</context:component-scan>
<mvc:annotation-driven />
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="${web.view.prefix}"/>
<property name="suffix" value="${web.view.suffix}"/>
</bean>
<mvc:resources mapping="/**/static/**" location="/static/" cache-period="31536000"/>
<mvc:interceptors>
<mvc:interceptor>
<mvc:mapping path="/**"/>
<mvc:exclude-mapping path="/login"/>
<mvc:exclude-mapping path="/static/**"/>
<mvc:exclude-mapping path="/loginOut"/>
<bean class="com.huahua.my.shop.web.admin.web.intercepter.LoginIntercepter" />
</mvc:interceptor>
<mvc:interceptor>
<mvc:mapping path="/login"/>
<bean class="com.huahua.my.shop.web.admin.web.intercepter.PermissionIntercepter" />
</mvc:interceptor>
</mvc:interceptors>
</beans>
这是UserController
类
@Controller
@RequestMapping(value = "/user")
public class UserController {
@Autowired
TbUserService tbUserService ;
@RequestMapping(value = "/list" , method = RequestMethod.GET)
public String userList(){
return "user_list" ;
}
}
这是我的拦截器
public class LoginIntercepter implements HandlerInterceptor {
public boolean preHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o ) throws Exception {
TbUser tbUser =(TbUser) httpServletRequest.getSession().getAttribute(ConstantUtils.SESSION_USER); //SESSION_USER = user
System.out.println(httpServletRequest.getRequestURL());
if (tbUser == null){
httpServletResponse.sendRedirect("login");
}
return true;
}
public void postHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, ModelAndView modelAndView) throws Exception {}
public void afterCompletion(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) throws Exception {}
}
这是LoginController
@Controller
public class LoginController {
@Autowired
private TbUserService tbuserService ;
@RequestMapping(value = {"","login"},method = RequestMethod.GET)
public String login(){
return "login" ;
}
@RequestMapping(value = "login",method = RequestMethod.POST)
public String login(@RequestParam(required = true) String email ,
@RequestParam(required = true ) String password ,
@RequestParam(required = false) String isRemember,
HttpServletRequest request,
Model model){
TbUser user = tbuserService.loginRight(email, password);
isRemember = isRemember == "on" ? "checked" : null ;
if (user != null ){
request.getSession().setAttribute(ConstantUtils.SESSION_USER,user);
request.getSession().setAttribute("remember",isRemember);
return "redirect:/main" ;
}
else {
model.addAttribute("message","username or password is wrong");
return "login" ;
}
}
http://localhost:8080/user/list
时,我成功输入了user_list.jsp
。一段时间之后,该会话时间已到,我刷新此页面,
the HTTP of refresh user_list page
我希望将其重定向到http://localhost:8080/login
,
但是我得到了这个路径http://localhost:8080/user/login
wrong with 404
我该如何解决这个错误?
为什么路径中还有一个'/ user'?
重定向路径中的/user
和UserController中的@RequestMapping(value = "/user")
之间是什么关系?
非常感谢您!
答案 0 :(得分:0)
如HttpServletResponse#sendRedirect
的API文档所述:
使用指定的地址向客户端发送临时重定向响应 重定向位置URL并清除缓冲区。缓冲区将是 用此方法替换为数据集。调用此方法集 状态代码发送到SC_FOUND 302(找到)。这种方法可以接受 相对网址; Servlet容器必须将相对网址转换为 发送响应到客户端之前的绝对URL。 如果 位置是相对的,没有前导“ /”,容器会对其进行解释 相对于当前请求URI 。如果位置是相对的 带有前导“ /”的容器会将其解释为相对于 servlet容器根。
因此,因为您已指定相对URL相对于当前请求URl /users/list
因此您需要将其更改为:
httpServletResponse.sendRedirect("/login");