I can not get the validation error messages to be showed in JSP page. Here is my controller:
@Controller
@RequestMapping("/secure")
@SuppressWarnings({"All"})
public class OperationController {
@ModelAttribute("crawler/operation")
public OperationForm operationForm() {
return new OperationForm();
}
@RequestMapping(value = "crawler/operation/create", method = RequestMethod.POST)
public String processCrawlerOperationForm(@Valid OperationForm operationForm, BindingResult result, Map model) {
if (result.hasErrors()) {
HashMap<String, String> errors = new HashMap<String, String>();
for (FieldError error : result.getFieldErrors()) {
errors.put(error.getField(), error.getDefaultMessage());
}
model.put("errors", errors);
return "crawler/operation";
}
//Some logic
return "crawler/operation";
}
}
I am 100% sure that errors exits. I have been debugging this code to be sure that there are errors.
My form class:
public class OperationForm {
@NotEmpty
private String operationName;
@NotEmpty
private String author;
@NotEmpty
private String configurationId;
public String getOperationName() {
return operationName;
}
//Getters and setters
}
My JSP spring form:
<form:form action="${pageContext.servletContext.contextPath}/secure/crawler/operation/create.htm" commandName="crawler/operation">
<table border="0" cellspacing="12">
<tr>
<td>
<spring:message code="application.operationForm.operationName"/>
</td>
<td>
<form:input path="operationName"/>
</td>
<td class="error">
<form:errors path="operationName"/>
</td>
</tr>
<tr>
<td>
<spring:message code="application.operationForm.author"/>
</td>
<td>
<form:input path="author"/>
</td>
<td class="error">
<form:errors path="author"/>
</td>
</tr>
<tr>
<td>
<spring:message code="application.operationForm.configuration"/>
</td>
<td>
<form:input path="configurationId"/>
</td>
<td class="error">
<form:errors path="configurationId"/>
</td>
</tr>
<tr>
<td>
<input class="button" type="submit" value="Vykdyti"/>
</td>
</tr>
</table>
</form:form>
What am I doing wrong?
Instead of looping through the list of errors and inserting them into a model object, just check validate and return the view i.e.
Also, in your controller method, add the @ModelAttribute annotation i.e.
Make sure the value of the ModelAttribute annotation matches the one you are putting into the model object when you’re displaying the form initially. If you’re using the method above operationForm to generate the model, set it to whatever that value is. I’d change that to a more usable value, Spring might get confused by the /, change it to something simple like “form”.