我无法使用@Valid注释获得国际化以验证错误。
<%@ page language="java" pageEncoding="ISO-8859-1"%>
<%@taglib prefix="form" uri="http://www.springframework.org/tags/form"%>
<%@ taglib prefix="spring" uri="http://www.springframework.org/tags" %>
<html>
<body>
<form:form action="processentry.do" method="post" commandName="entryForm">
<TABLE >
<TBODY>
<TR>
<TD><spring:message code="messages.name" text="Name" /></TD>
<TD><INPUT type="text" name="name" value="${ entryForm.name }" size="20"></TD>
<td><font color="red"><form:errors path="name" /></font></td>
</TR>
<TR>
<TD></TD>
<TD><INPUT type="submit" value="Submit"></TD>
</TR>
</TBODY>
</TABLE>
<br />
Current Locale : ${pageContext.response.locale}
</form:form>
</body>
我的表格
public class EntryForm {
@NotEmpty("required")
@Size(min = 1, max = 20)
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
控制器:
@Controller
public class EntryController {
@RequestMapping(value ="/processentry.do", method = RequestMethod.POST)
public String entry(@Valid EntryForm entryForm, BindingResult result, Map model) {
if (result.hasErrors()) {
return "entry.jsp";
}
model.put("entryForm", entryForm);
return "showresult.jsp";
}
@RequestMapping(value ="/entry.do", method = RequestMethod.GET)
public String entry(Map model){
EntryForm entryForm = new EntryForm();
model.put("entryForm", entryForm);
return "entry.jsp";
}
}
弹簧配置文件:
<mvc:annotation-driven />
<context:component-scan base-package="controllers" />
<!-- Register the messages.properties -->
<bean id="messageSource"
class="org.springframework.context.support.ResourceBundleMessageSource">
<property name="basename" value="resources.messages" />
</bean>
我支持2种语言:英语和荷兰语:
messages_en.properties:
messages.name = Name
messages_nl.properties:
messages.name = Naam
我还希望在验证错误期间出现荷兰错误消息。 我尝试在属性文件中添加消息,但这不起作用。
messages_nl.properties:
messages.name = Naam
javax.validation.constraints.Size.message=lengte moet tussen {min} en {max} zijn
org.hibernate.validator.constraints.NotEmpty.message=mag niet leeg zijn
我的问题是,如何显示自己的验证错误消息?