【问题标题】:"HTTP Status 404" and there is an extra "wrong path" in request path“HTTP 状态 404”,请求路径中有一个额外的“错误路径”
【发布时间】:2020-03-20 21:15:09
【问题描述】:

这是我练习的 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>

这是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" ;
        }

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

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

  3. 我该如何解决这个错误?

  4. 为什么路径中有额外的“/用户”? 重定向路径中的/user 和UserController 中的@RequestMapping(value = "/user") 有什么关系?

  5. 非常感谢!!!

【问题讨论】:

    标签: spring-mvc http-status-code-404


    【解决方案1】:

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

    使用指定的向客户端发送临时重定向响应 重定向位置 URL 并清除缓冲区。缓冲区将是 替换为该方法设置的数据。调用此方法集 SC_FOUND 302 (Found) 的状态码。这个方法可以接受 相对 URL;servlet 容器必须将相对 URL 转换为 在向客户端发送响应之前的绝对 URL。 如果 位置是相对的,没有前导“/”,容器会解释它 相对于当前请求 URI。如果位置是相对的 带有前导“/”的容器将其解释为相对于 servlet 容器根目录。

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

    所以你需要把它改成:

    httpServletResponse.sendRedirect("/login");
    

    【讨论】:

      猜你喜欢
      • 2022-01-14
      • 2015-09-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-09-18
      • 1970-01-01
      相关资源
      最近更新 更多