用于 bean 验证的自定义验证消息
我正在创建一个 JSF 2 应用程序,并且尝试在 bean 中而不是 faces-page 中使用表单验证。我还想使用 .properties 文件来存储消息。
我看了这个问题但我不认为我正确设置属性文件。
假设我在名为“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的博客?
提前致谢!
/ 丹尼斯
I'm creating a JSF 2-application and I'm trying to use form validation in the bean instead of the faces-page. I'd also like to use a .properties file to store the messages.
I looked at this question but I don't think I have the properties-file set up correctly.
Let's say I have a bean called User in a package called 'dev':
@ManagedBean
@SessionScoped
public class User implements Serializable {
@Pattern(pattern=".+@.+\\.[a-z]+", message="{dev.User.emailAddress}")
private String emailAddress;
// getter/setter
}
I've also created a file 'ValidationMessages.properties' in WEB-INF/classes (I'm using Netbeans 7.0.1)
In the ValidationMessages.properties file I've got this key/value-line:
dev.User.emailAddress=Custom invalid email message
And my view (user.xhtml) looks like this:
<!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>
When I enter an invalid email and press the button, the validation message in the web page reads:
{dev.User.emailAddress}
instead of
Custom invalid email message
Could the problem be that I haven't registered my properties file in web.xml?
I should switch required="true" to @NotNull in the bean too. Is there anything more I need to do than insert this:
<context-param>
<param-name>javax.faces.INTERPRET_EMPTY_STRING_SUBMITTED_VALUES_AS_NULL</param-name>
<param-value>true</param-value>
</context-param>
as explained in BalusC's blog?
Thanks in advance!
/ Dennis
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我知道您已将其直接放入项目的
/WEB-INF/classes
文件夹是你自己的吗?如果您让 IDE 构建/部署 WAR,这可能会被忽略/覆盖(至少对于 Eclipse 来说是这样)。您应该将该文件直接放在项目的 Java 源文件夹的根目录中。在将项目导出到 WAR 文件并使用某些 ZIP 工具将其解压后,您可以自行验证它是否确实位于/WEB-INF/classes
中。I understand that you've put it straight in the project's
/WEB-INF/classes
folder yourself? This will likely be ignored/overridden if you let your IDE build/deploy the WAR (at least, this is true for Eclipse). You should rather put that file straight in the root of the Java source folder of the project. You can verify yourself if it really ended up in/WEB-INF/classes
, after you export the project to a WAR file and then extract it using some ZIP tool.我将 .properties 文件放在资源文件夹中并且它有效,无需在任何地方注册该文件。郑重声明:我正在使用 Netbeans 7.0.1 和 Maven。
感谢您的帮助,BalusC!
I placed the .properties file in the resources folder and it worked, there was no need to register the file anywhere. For the record: I'm using Netbeans 7.0.1 and Maven.
Thanks for the help, BalusC!