我发送以下 http 请求:
http://localhost:8081/member/createCompany/getSmallThumbnail/
在服务器端,我进入了 Controller 方法:
@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。
但是我在请求中找不到返回这个的方法。
请帮忙返回我想要的url。
附言
实际上,我还没有在我的 spring mvc 应用程序(url 已损坏)中为 http://localhost:8081/member/createCompany/getSmallThumbnail/
映射 Controller 。此 url("/error") 在 web.xml 中配置为错误页面。
最佳答案
你的请求被重新发送到 /error
(可能是为了错误处理)。
如果此框架遵循正常的 Servlet 错误调度行为,那么您的原始请求可以在各种 javax.servlet.RequestDispatcher.ERROR_*
下的 HttpServletRequest.getAttributes()
中找到。键。
ERROR_EXCEPTION
- 异常对象ERROR_EXCEPTION_TYPE
- 异常对象的类型ERROR_MESSAGE
- 异常消息ERROR_REQUEST_URI
- 导致错误分派(dispatch)的原始请求 uriERROR_SERVLET_NAME
- 导致错误的 servlet 的名称ERROR_STATUS_CODE
- 为此错误分派(dispatch)确定的响应状态代码
你想要的是
String originalUri = (String) request.getAttribute(
RequestDispatcher.ERROR_REQUEST_URI)
关于spring-mvc - 如何找出请求到达错误处理程序的 url?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32910492/