Is it possible for a Spring controller to handle both kind of requests?
1) http://localhost:8080/submit/id/ID123432?logout=true
2) http://localhost:8080/submit/id/ID123432?name=sam&password=543432
If I define a single controller of the kind:
@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 {...}
the HTTP request with "logout" is not accepted.
If I define two controllers to handle each request separately, Spring complains with the exception "There is already 'Controller' bean method … mapped".
Best Answer
You need to give
required = false
forname
andpassword
request parameters as well. That's because, when you provide just thelogout
parameter, it actually expects forname
andpassword
as well as they are still mandatory.It worked when you just gave
name
andpassword
becauselogout
wasn't a mandatory parameter thanks torequired = false
already given forlogout
.