2013-02-13 51 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