未使用的URI模板变量 - 我可以用通配符替换它吗?

时间:2012-03-07 18:10:06

标签: java spring spring-mvc annotations

我的Spring MVC webapp中包含以下代码:

@RequestMapping("/{someVariable}/aPath/aPage.do")
public void serveAPage() {
    doStuff();
}

我们希望“someVariable”在URL中,但我们对捕获和使用它的值不感兴趣。有没有办法用通配符替换它,例如/*/aPath/aPage.do

1 个答案:

答案 0 :(得分:1)

是的,@ RequestMapping接受来自http://static.springsource.org/spring/docs/3.0.x/javadoc-api/org/springframework/web/bind/annotation/RequestMapping.html#params()

的Ant样式模式

这样可行:

@RequestMapping(value="/*/test2.do")
public void getMeta5(HttpServletRequest request, HttpServletResponse response) throws IOException {
    final PrintWriter writer = response.getWriter();
    writer.print("requestURI:" + request.getRequestURI());
    writer.flush();
}

这假设web.xml中的servlet映射将该路径映射到DispatcherServlet,例如

<servlet>
    <servlet-name>spring</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
    <servlet-name>spring</servlet-name>
    <url-pattern>*.do</url-pattern>
</servlet-mapping>