2

我发送以下http请求:

http://localhost:8081/member/createCompany/getSmallThumbnail/

在服务器端,我进入了控制器方法:

@RequestMapping("/error")
public String error(Model model, HttpServletRequest request){
    if(request.getRequestURI().contains("thumbnail")){
        System.out.println("thumbnail accepted");
     }
     request.toString();
     model.addAttribute("message", "page not found");
     return "errorPage";
}

在这种方法中,我想知道请求到达的 url。

如果在调试中我在此方法内停止,我会看到我需要的信息: 在此处输入图像描述

但我在请求中找不到将返回此的方法。

请帮助返回我想要的网址。

附言

实际上,我没有在我的 spring mvc 应用程序(url 已损坏)中为http://localhost:8081/member/createCompany/getSmallThumbnail/. 此 url("/error") 在 web.xml 中配置为错误页面。

4

1 回答 1

9

您的请求被重新发送到/error(可能是为了错误处理)。

如果这个框架遵循正常的 Servlet 错误调度行为,那么你的原始请求可以在HttpServletRequest.getAttributes()各种javax.servlet.RequestDispatcher.ERROR_*键下找到。

  • ERROR_EXCEPTION- 异常对象
  • ERROR_EXCEPTION_TYPE- 异常对象的类型
  • ERROR_MESSAGE- 异常消息
  • ERROR_REQUEST_URI- 导致错误调度的原始请求 uri
  • ERROR_SERVLET_NAME- 导致错误的 servlet 的名称
  • ERROR_STATUS_CODE- 为此错误调度确定的响应状态代码

你想要的是

String originalUri = (String) request.getAttribute(
                                       RequestDispatcher.ERROR_REQUEST_URI)
于 2015-10-02T15:42:49.350 回答