首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >HttpServletRequest -获取文字路径

HttpServletRequest -获取文字路径
EN

Stack Overflow用户
提问于 2015-09-17 21:12:15
回答 1查看 251关注 0票数 0

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

如果在路径为"request.getServletPath()“时输出对"/things/{thingId}”的调用结果,则将得到"/things/2489sdfjk43298f“,其中{thingId} path参数已被实际值替换。

我想打印出文字请求路径"/things/{thingId}";也就是说,使用花括号的路径参数“{thingId}”。

这有可能吗?

编辑:在看了Sotirios下面的第二个评论之后,我意识到我可能是在向后看这个问题。这就是我真正想做的..。

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

在使用path参数到达第一个端点之前,这一切都进行得很顺利,因为我显然不能将"/things/2489sdfjk43298f“值放入枚举中,因为这将只匹配一个特定的thing和那个特定的ID。

所以,也许实际的问题是:当路径的某些部分由于路径参数而发生变化时,我将如何进行查找?我是否可以使用某种包含通配符的String格式?

我想这更像是一个enum-lookup/String-matching问题。是我的错。

编辑2:缩短了我所说的enum的例子:

代码语言:javascript
复制
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);
  }
}

缩短了我的端点示例:

代码语言:javascript
复制
@RequestMapping(value = "/**", method = RequestMethod.GET)
public ModelAndView showPage(HttpServletRequest request) {
  return new ModelAndView(JspEndpointType.getByRequestPath(request.getServletPath()));
}

因此,本质上可以归结为试图向enum添加如下值:

代码语言:javascript
复制
THINGS("/things/{thingId}", "jsp/things/whatever")

然后,..and能够通过路径"/things/2489sdfjk43298f“并返回”/jsp/things/whatever“。

编辑3: --我找到了指向Spring的UriComponentsBuilder,特别是"fromPath“方法的这个StackoverFlow问题。然而,这似乎与我想要做的相反.

EN

回答 1

Stack Overflow用户

发布于 2015-09-17 21:17:37

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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/32640059

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档