用于 bean 验证的自定义验证消息

发布于 2024-12-14 09:20:52 字数 1896 浏览 4 评论 0原文

我正在创建一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

十年九夏 2024-12-21 09:20:52

我还在 WEB-INF/classes 中创建了一个文件“ValidationMessages.properties”(我使用的是 Netbeans 7.0.1)

我知道您已将其直接放入项目的 /WEB-INF/classes 文件夹是你自己的吗?如果您让 IDE 构建/部署 WAR,这可能会被忽略/覆盖(至少对于 Eclipse 来说是这样)。您应该将该文件直接放在项目的 Java 源文件夹的根目录中。在将项目导出到 WAR 文件并使用某些 ZIP 工具将其解压后,您可以自行验证它是否确实位于 /WEB-INF/classes 中。

I've also created a file 'ValidationMessages.properties' in WEB-INF/classes (I'm using Netbeans 7.0.1)

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.

懵少女 2024-12-21 09:20:52

我将 .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!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文