"HTTP Status 404" 请求路径中多了一个 "wrong path"

"HTTP Status 404" and there is an extra "wrong path" in request path

这是我的实践Spring项目。我无法重定向到 SpringMVC 拦截器中的正确页面。

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>

这是UserControllerclass

@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" ;
        }

    }
  1. 当我登录并请求http://localhost:8080/user/list时,我成功进入了user_list.jsp
  2. 过了一段时间,这个session没时间了,我刷新这个页面, the HTTP of refresh user_list page 我希望它被重定向到 http://localhost:8080/login

    但我得到了这条路 http://localhost:8080/user/login
    wrong with 404

  3. 这个错误怎么解决?

  4. 为什么路径中多了一个'/user'? 重定向路径中的 /user 和 UserController 中的 @RequestMapping(value = "/user") 之间的关系是什么?

  5. 非常感谢!!!

作为 API 文档对 HttpServletResponse#sendRedirect 的说明:

Sends a temporary redirect response to the client using the specified redirect location URL and clears the buffer. The buffer will be replaced with the data set by this method. Calling this method sets the status code to SC_FOUND 302 (Found). This method can accept relative URLs;the servlet container must convert the relative URL to an absolute URL before sending the response to the client. If the location is relative without a leading '/' the container interprets it as relative to the current request URI. If the location is relative with a leading '/' the container interprets it as relative to the servlet container root.

所以因为你指定了一个相对的 URL 是相对于当前请求解析的 URl /users/list

因此您需要将其更改为:

httpServletResponse.sendRedirect("/login");