Spring控制器可以處理這兩種請求嗎?Spring MVC中的@RequestParam處理可選參數
1) http://localhost:8080/submit/id/ID123432?logout=true
2) http://localhost:8080/submit/id/ID123432?name=sam&password=543432
如果我定義的那種單個控制器:
@RequestMapping (value = "/submit/id/{id}", method = RequestMethod.GET,
produces="text/xml")
public String showLoginWindow(@PathVariable("id") String id,
@RequestParam(value = "logout", required = false) String logout,
@RequestParam("name") String username,
@RequestParam("password") String password,
@ModelAttribute("submitModel") SubmitModel model,
BindingResult errors) throws LoginException {...}
HTTP請求與 「註銷」 是不能接受的。
如果我定義了兩個控制器分別處理每個請求,Spring會抱怨「已經有'Controller'bean方法...映射」的異常。
當有人將註銷,名稱和密碼都傳遞給URL時會發生什麼?只要閱讀文檔,它說'!myParam樣式表達式表明指定的參數不應該出現在請求中。「必須嘗試。 –
它會找到最好的匹配,它可能會嘗試使用'handleLogin',否則它會提供一個異常,說明不能找到映射。 –
只需要注意一點:從安全角度來看,註銷應該只接受POST請求,所以_should_有2種方法,並且保持URL不變是毫無意義的。 –