2016-05-30 126 views
0

我有一個控制器,並且我在它上面放了雙斜槓。Spring MVC - RequestMapping中的雙斜槓導致CSS鏈接中的錯誤

@RequestMapping(value="/vehicles/add", method = RequestMethod.GET) 
    public String addVehicle(Model model) { 
    model.addAttribute("vehicle", new Vehicle()); 
    return "newvehicle"; 
} 

而CSS鏈接:

<link rel="stylesheet" type="text/css" href="css/main.css"/> 

發生了什麼事是,當我有雙斜線的HTML的CSS鏈接不能正常工作。所以我試圖用單斜槓

RequestMapping(value="/add", method = RequestMethod.GET) 

它工作。但我想要一個雙斜槓。你認爲是什麼問題。我認爲目錄不是問題,因爲它使用單斜槓。

+1

它沒有任何用雙斜線或單斜線做。您不應該使用絕對鏈接爲樣式表使用相對鏈接。 –

+0

@ M.Deinum我該怎麼做? – Lester

+0

使用該標記的URL標記,可以是Spring或JSTL庫中的標記。 –

回答

0

我認爲你誤解了Spring MVC的@RequestMapping註釋的含義。

您在註釋中提到的value實際上是將傳遞的URI,以便調用給定的方法。

例如,當您這樣寫:

@RequestMapping(value="/vehicles/add", method = RequestMethod.GET) 
    public String addVehicle(Model model) { 
... 
... 
} 

這意味着當你點擊URL - http://server:port/<context-root>/<web-services-endpoint>/<rest-endpoint>/vehicles/add,用GET方法,你的API addVehicle將被調用。

如果您在@RequestMapping爲相同的API提value="/add",當你打這個它會被稱爲:http://server:port/<context-root>/<web-services-endpoint>/<rest-endpoint>/add

請參考春天文檔的更多信息這個: http://docs.spring.io/autorepo/docs/spring/3.2.x/spring-framework-reference/html/mvc.html