编程语言
首页 > 编程语言> > java-如何知道@RequestMapping的哪个参数被调用

java-如何知道@RequestMapping的哪个参数被调用

作者:互联网

这是我的@RequestMapping批注:

  @RequestMapping({"/loginBadCredentials", "/loginUserDisabled", "/loginUserNumberExceeded"})
  public String errorLogin(...){        
            ... 
        }

在errorLogin方法内部,是否有办法知道三个网址中的哪个被“调用”了?

解决方法:

添加HttpServletRequest作为您的参数,并使用它来查找当前的请求路径.

更新:Spring还提供了RequestContextHolder:

ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.currentRequestAttributes();
String currentReqUri = attributes.getRequest().getRequestURI();

我认为,第一种方法更好,并且更具可测试性.

标签:request-mapping,java,spring,spring-mvc
来源: https://codeday.me/bug/20191011/1891261.html