Spring控制器是否可以处理两种请求?
1) http://localhost:8080/submit/id/ID123432?logout=true
http://localhost:8080/submit/id/ID123432?logout=true
2) http://localhost:8080/submit/id/ID123432?name=sam&password=543432
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方法…映射”异常。
你需要给required = false的name,并password请求参数也是如此。这是因为,当您仅提供logout参数时,它实际上是预期的name并且password仍然是必需的。
required = false
name
password
logout
当您刚刚给定时,它就起作用了name,password因为logout这不是必需参数,这要归功于required = falsefor logout。