Request method 'POST' not supported
this error spring throws when following things are wrong:
- HTML form action has some URL with method as
POST
and you don't have any controller methods to handle POST request to that URL. - missing or invalid
@ModelAttribute
in POST handler method.
coming to
the backing object doesn't seem to set the name field here:
inside <spring:bind
you should use status
object like:
status.value
: to get the actual value of the bean or property.status.expression
: the expression that was used to retrieve the bean or property.status.errorMessages
: an array of error messages, resulting from validation
And No need to use <spring:bind..
when you use <form:form..
tag, both are almost does the same job, while <spring:bind..
is used to perform some customized operations. So choose either one in <form:sorm..
& <spring:bind..
in your case you can fix you problem using <spring:bind..
like:
<c:url value="/indexBackingUrl" var="pstUrl"/>
<form action="${pstUrl}" method="post" >
<table id="main">
<tr>
<td></td>
<td>Name:
<spring:bind path="indexBacking.name">
<input type="text"
name="${status.expression}"
value="${status.displayValue}"/>
<c:if test="${status.error}">
Error codes:
<c:forEach items="${status.errorMessages}" var="error">
<c:out value="${error}"/>
</c:forEach>
</c:if>
</spring:bind>
</td>
<td>
<input type="submit" value="Submit" />
</td>
<td></td>
</tr>
</table>
</form>
NOTE: name
attribute is important in input element, cause spring binds bean property value using name of input element.
OR using <form:form..
without <spring:bind..
like:
<form:form action="${pstUrl}" method="post" modelAttribute="indexBacking">
<form:label path="name">Name:</form:label>
<form:input path="name"/>
<form:errors path="name" element="div"/>
</form:form>
and in Controller's GET handler method add IndexBacking
bean instance with key as indexBacking
to make work <spring:bind..
in jsp like:
@RequestMapping(value="/indexBackingUrl", method = RequestMethod.GET)
public String handleNext(Model model) {
model.addAttribute("indexBacking", new IndexBacking("Jon"));
return "indexBacking";
}
and POST handler method in controller will look like:
@RequestMapping(value="/indexBackingUrl", method = RequestMethod.POST)
public String handleNextPost(ModelMap map, HttpServletRequest request,
@ModelAttribute("indexBacking") IndexBacking bo, BindingResult result) {
System.out.println(bo);
return "redirect:/someOtherUrl";
}
Edit: when you use enctype="multipart/form-data"
in form, then you should register CommonsMultipartResolver
bean and commons-io-x.x.jar
& commons-fileupload-x.x.x.jar
should be available to make work of spring binding or else you will get null
value, In your case enctype="multipart/form-data"
is not need as you not using any file upload etc.
See Also: