I want to do a mcq with Spring mvc. I’ve got a class Mcq with a OneToMany relationship with the class Question, which has a OneToMany relationship with the class Answer. Thus Mcq have as property an Arraylist ListOfQuestions, and Question an Arraylist ListOfAnswers.
My controller is
@RequestMapping(value="displayMcq", method = RequestMethod.GET)
public String showMcq(Model model) {
Mcq mcq = mcqService.findById(new Long(1));
model.addAttribute("mcq", mcq);
return "displayMcq";
}
@RequestMapping(method = RequestMethod.POST)
public String displayQcmRepondu(@ModelAttribute("mcq2") Mcq mcq, BindingResult binding, SessionStatus status) {
if (binding.hasErrors()) {
return "displayMcq";
} else {
status.setComplete();
return "redirect:/mcqSuccess/";
}
}
and my view displayMcq.jsp is
<form:form modelAttribute="mcq" method="POST">
<ol>
<c:forEach items="${mcq.listOfQuestions}" var="question">
<li>
<c:out value="${question.label}" />
<br />
<ul>
<c:forEach var="answer" items="${question.listOfAnswers}">
<form:checkbox path="listOfQuestions" value="answer.id" label="${answer.label}" />
<br />
</c:forEach>
</ul>
</li>
</c:forEach>
</ol>
<input type="submit" value="Validate" />
</form:form>
My mcq is well displayed but the processing of the form fails. I stay on the displayMcq appearance with the error “Etat HTTP 405 – Request method ‘POST’ not supported”.
So, could you explain me the problem, help me to proccess my mcq correctly and return the checked answers?
Note that your controller methods are mapped to different URLs (due to absence of
valueattribute on your POST method).Since you don’t have
actionattribute in<form:form>, it sends a POST request to the current page’s URL on submit, but you don’t have controller methods to handle POST request to that URL.So, you need to map your POST method to the same URL as your GET method: