Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/spring-mvc/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Spring mvc HttpServletRequest-获取文本路径_Spring Mvc_Servlets - Fatal编程技术网

Spring mvc HttpServletRequest-获取文本路径

Spring mvc HttpServletRequest-获取文本路径,spring-mvc,servlets,Spring Mvc,Servlets,我有一个用Spring的@RequestMapping标记的方法,其中包括一个HttpServletRequest方法参数 如果在路径为/things/{thingId}时打印request.getServletPath调用的结果,我将得到/things/2489sdfjk43298f,其中{thingId}path参数已替换为实际值 我想打印出文本请求路径/things/{thingId};也就是说,使用花括号,未替换的路径参数{thingId} 这有可能吗 编辑:在看了索蒂里奥斯下面的第二条

我有一个用Spring的@RequestMapping标记的方法,其中包括一个HttpServletRequest方法参数

如果在路径为/things/{thingId}时打印request.getServletPath调用的结果,我将得到/things/2489sdfjk43298f,其中{thingId}path参数已替换为实际值

我想打印出文本请求路径/things/{thingId};也就是说,使用花括号,未替换的路径参数{thingId}

这有可能吗

编辑:在看了索蒂里奥斯下面的第二条评论后,我意识到我可能是在向后看问题。这是我真正想做的

我试图在/**下创建一个端点,该端点从HttpServletRequest获取路径,我使用它在枚举中查找值。这个枚举有几个字段,其中一个显然是前面提到的路径,但另一个是目标JSP文件的路径。然后,我将此路径放入ModelAndView对象中,并返回它以显示页面

在我使用path参数命中第一个端点之前,这一切都很顺利,因为我显然无法将值/things/2489sdfjk43298f放入枚举中,因为这将只匹配具有特定ID的特定对象

因此,实际的问题可能是:当路径的某些部分由于路径参数而改变时,我将如何进行查找?是否有某种包含字符串格式的通配符可以使用

我想这更多的是一个枚举查找/字符串匹配问题。我的错

编辑2:我所说的枚举的简化示例:

public enum JspEndpointType {
  HOME("/home", "jsp/home");

  private static final Map<String, String> pathMap;
  private String requestPath;
  private String jspPath;

  static {
    pathMap = new HashMap<>();
    for (JspEndpointType jspEndpointType : JspEndpointType.values()) {
      pathMap.put(jspEndpointType.getRequestPath(), jspEndpointType.getJspPath());
    }
  }

  private JspEndpointValue(String requestPath, String jspPath) {
    this.requestPath = requestPath;
    this.jspPath = jspPath;
  }

  public String getRequestPath() {
    return requestPath;
  }

  public String getJspPath() {
    return jspPath;
  }

  public static String getByRequestPath(String requestPath) {
    return pathMap.get(requestPath);
  }
}
因此,本质上可以归结为尝试向枚举添加如下值:

THINGS("/things/{thingId}", "jsp/things/whatever")
…然后能够通过路径/things/2489sdfjk43298f并返回/jsp/things/which


编辑3:我找到了Spring的方法,特别是fromPath方法。然而,这似乎与我试图做的相反…

您可以使用反射自己查找@RequestMapping注释。

为什么要这样做?你已经知道它是{thingId}。这在您的源代码中是一个常量。@SotiriosDelimanolis,因为它在我的源代码中不是常量。my@RequestMapping中的value属性是通配符/**。那么就没有要查找的占位符了。请澄清。另外,路径变量由Spring dispatcher servlet处理。servlet API完全不知道它们的存在。这看起来像是XY问题。在更高的层次上,你想达到什么目的?@SotiriosDelimanolis,JBNizet稍微更新了这个问题。另外:你不能在评论中通知多个人,这有点糟糕。
THINGS("/things/{thingId}", "jsp/things/whatever")