我无法在 JSP 页面中显示验证错误消息。这是我的控制器:
@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";
}
}
我 100% 确定错误存在。我一直在调试这段代码以确保有错误。
我的表单类:
public class OperationForm {
@NotEmpty
private String operationName;
@NotEmpty
private String author;
@NotEmpty
private String configurationId;
public String getOperationName() {
return operationName;
}
//Getters and setters
}
我的 JSP 弹簧形式:
<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>
我究竟做错了什么?