2013-02-13 90 views
0

我有一个使用Spring MVC的Web应用程序。Spring请求映射:与url模式匹配

的web.xml

<servlet> 
    <servlet-name>spring</servlet-name> 
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> 
</servlet> 

<servlet-mapping> 
    <servlet-name>spring</servlet-name> 
    <url-pattern>*.do</url-pattern> 
    <url-pattern>/companies/*</url-pattern> 
</servlet-mapping> 

春天控制器的方法:

class RealmInfoController{ 

    @ResponseBody 
    @RequestMapping(value = {"/companies/{companyId}/realms/{realmName}"}) 
    public RealmInfo realmInfo(@PathVariable long companyId, @PathVariable String realmName) 

处理器搭配:

http://localhost:6122/context/companies/15877/realms/firstRealm 

当服务器获取此URL,弹簧的servlet被调用。但它无法匹配控制器方法。

但是,如果我将请求映射更改为“/ {companyId}/realms/{realmName}”,那么它与控制器方法相匹配。但是定义没有'/ companies'的URL映射并不好。 Spring能否以某种方式指示查找包含servlet中指定的url模式的匹配项?

谢谢。

如果你想使用“公司”的请求映射您应该在调度员的servlet映射到根
+2

'有'/公司/ *'路径,你告诉只要URL匹配定义的模式,容器就会调用_DispatherServlet_。所以在控制器中,你不能在_ @ RequestMapping_中包含'/ companies /'部分 - **它已经包含了**。换句话说,在_ @ RequestMapping_中不包含'/ companies /'部分**是正确的方法**。 – informatik01 2013-02-13 15:51:55

回答

1

:因为`

<url-pattern>/*</url-pattern> 
+0

+1。可能是满足OP需求的最接近的解决方案。 – informatik01 2013-02-13 15:46:37

+0

我无法使用/*/,因为它隐藏了其他servlet和错误页面定义。 – Rag 2013-02-13 15:59:56

+1

@Rag然后就像你以前那样做。正如我在上面的评论中写到的 - 不包括'@ RequestMapping'中的'/ companies /'在你的案例中是正确的。 – informatik01 2013-02-13 16:06:10