I am using 3.1.1 here.
This works fine, the form is dislayed correctly
@RequestMapping(value={"/universities"}, params="new", method=RequestMethod.GET)
public String addUniversity(Model model) {
model.addAttribute("addForm", AddForm.newUniversity());
return "page/add-university";
}
When testing the validation error (by emptying the fields), this results in 500 internal server error, and displays stacktraces with the main message : Neither BindingResult nor plain target object for bean name 'addForm' available as request attribute
This is the method :
@RequestMapping(value={"/universities"}, method=RequestMethod.POST)
public String submitNewUniversity(@Valid AddForm form, BindingResult binding) {
if (binding.hasErrors()) {
return "page/add-university";
}
// do others here ...
}
Here is my JSP :
<%@ taglib prefix="s" uri="http://www.springframework.org/tags" %>
<%@ taglib prefix="t" uri="http://tiles.apache.org/tags-tiles" %>
<%@ taglib uri="http://java.sun.com/jstl/core_rt" prefix="c" %>
<%@ taglib prefix="sf" uri="http://www.springframework.org/tags/form"%>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"
session="false" %>
<s:url var="action" value="/universities" />
<sf:form method="POST" modelAttribute="addForm" action="${action}">
<fieldset>
<table cellspacing="0">
<tr>
<th><sf:label path="name">University:</sf:label></th>
<td><sf:input path="name" size="30" /> <br/>
<sf:errors path="name" cssClass="error" />
</td>
</tr>
.....
</sf:form>
So far what i've researched are these :
- The bean name is correct, which is "addForm", both in the controller and the JSP tile.
- The view exists, since displaying the form works fine.
- The field names in the JSP are also correct, since displaying the form works fine.
- The order of parameters are fine, BindingResult follows the command bean
- I am sure that the submitNewUniversity is called by the output of the logs i added
I wonder what i did wrong here ?