【发布时间】:2011-12-24 07:47:09
【问题描述】:
我正在创建一个 JSF 2 应用程序,并尝试在 bean 中使用表单验证,而不是在 faces-page 中。我还想使用 .properties 文件来存储消息。
我查看了this question,但我认为我的属性文件设置不正确。
假设我在一个名为“dev”的包中有一个名为 User 的 bean:
@ManagedBean
@SessionScoped
public class User implements Serializable {
@Pattern(pattern=".+@.+\\.[a-z]+", message="{dev.User.emailAddress}")
private String emailAddress;
// getter/setter
}
我还在 WEB-INF/classes 中创建了一个文件“ValidationMessages.properties”(我使用的是 Netbeans 7.0.1)
在 ValidationMessages.properties 文件中,我有这个键/值行:
dev.User.emailAddress=Custom invalid email message
我的视图(user.xhtml)看起来像这样:
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://java.sun.com/jsf/html">
<h:head>
<title>User registration</title>
</h:head>
<h:body>
<h:form>
<h:inputText id="emailAddress" value="#{user.emailAddress}" required="true" />
<h:message for="emailAddress" />
<h:commandButton value="Ok" action="null" />
</h:form>
</h:body>
当我输入无效的电子邮件并按下按钮时,网页中的验证消息显示:
{dev.User.emailAddress}
而不是
Custom invalid email message
问题可能是我没有在 web.xml 中注册我的属性文件吗?
我也应该在 bean 中将 required="true" 切换为 @NotNull。除了插入这个,我还需要做什么:
<context-param>
<param-name>javax.faces.INTERPRET_EMPTY_STRING_SUBMITTED_VALUES_AS_NULL</param-name>
<param-value>true</param-value>
</context-param>
如BalusC's blog中所述?
提前致谢!
/丹尼斯
【问题讨论】:
标签: java jsf-2 properties bean-validation