这是我的@RequestMapping
注释:
@RequestMapping({"/loginBadCredentials", "/loginUserDisabled", "/loginUserNumberExceeded"})
public String errorLogin(...){
...
}
在方法 errorLogin 中,有没有办法知道三个 url 中的哪一个被“调用”?
最佳答案
添加 HttpServletRequest
作为参数并使用它来查找当前请求路径。
更新:Spring还提供了RequestContextHolder
:
ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.currentRequestAttributes();
String currentReqUri = attributes.getRequest().getRequestURI();
在我看来,第一种方法更好并且更容易测试。
关于java - 如何知道@RequestMapping的哪个参数被调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34919809/