如何知道@RequestMapping的哪个参数被调用 [英] How to know which param of @RequestMapping is called

查看:371
本文介绍了如何知道@RequestMapping的哪个参数被调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的@RequestMapping批注:

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

在方法 errorLogin 中,有没有办法知道三个网址中的哪个被调用"了?

Inside the method errorLogin , is there a way to know which of the three url was "called"?

推荐答案

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

Add HttpServletRequest as your parameters and use it to find the current request path.

更新:Spring还提供了RequestContextHolder:

Update: Spring also provides RequestContextHolder:

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

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

In my opinion, first approach is better and a little more testable.

这篇关于如何知道@RequestMapping的哪个参数被调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆