我使用一个实现Bean验证的输入字段制作一个简单的表单。所有的验证工作正常。问题是(或缺乏渲染),当用户输入一个非数值呈现消息:在@Pattern Bean验证中没有呈现任何消息
如果我使用@Pattern检查在Bean,当用户在视图中显示警告信息输入一个非数字值。
如果我从bean取出@Pattern,下面的警告信息显示出来,当用户进入非数字小值:
“j_idt8:年龄:‘一些字符串’德贝SER未NÚMERO恩特雷里奥斯-2147483648 y 2147483647. Ejemplo:9346“
它被翻译为:”j_idt8:age:'一些字符串'必须是介于-2147483648和2147483647之间的数字。例如:9346“。
有什么我失踪了吗?有什么方法可以实现简单的消息警告吗?或者无论如何要定制默认消息:“j_idt8:age:'some string'debe ser un unnúmeroentre -2147483648 y 2147483647. Ejemplo:9346”?
这些都是一些代码片段,如果你问他们:
的web.xml
<context-param>
<description>JSF will interpret empty strings submitted values as null</description>
<param-name>javax.faces.INTERPRET_EMPTY_STRING_SUBMITTED_VALUES_AS_NULL</param-name>
<param-value>true</param-value>
</context-param>
用户的Bean:
@NotNull(message = "Please enter your age")
@Pattern(regexp = "[0-9][0-9]", message = "Age must be numeric")
@Max(99)
@Min(18)
private Integer age;
的Facelets:
<h:form>
Enter your age:<br/>
<h:inputText id="age" value="#{user.age}">
<f:ajax event="blur" render="ageMessage" />
</h:inputText>
<h:message id="ageMessage" for="age" /><br/>
<h:commandButton value="Insert" action="#{user.showChoice}">
<f:ajax execute="@form" render="@form" />
</h:commandButton>
</h:form>
感谢您的快速回复。我会寻找覆盖默认消息包。 – 2012-01-10 16:53:55
这是'javax.faces.converter.IntegerConverter.INTEGER_detail'的关键。你可以在JSF2规范的2.5.2.4章中找到它们。您可以在JSF API JAR文件的'javax.faces.Messages'包中找到所有缺省值。 – BalusC 2012-01-10 16:55:35