简体   繁体   English

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

[英]Custom validation message for bean validation

I'm creating a JSF 2-application and I'm trying to use form validation in the bean instead of the faces-page. 我正在创建一个JSF 2应用程序,我正在尝试在bean中使用表单验证而不是faces-page。 I'd also like to use a .properties file to store the messages. 我还想使用.properties文件来存储消息。

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': 假设我在名为'dev'的包中有一个名为User的bean:

@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) 我还在WEB-INF / classes中创建了一个文件'ValidationMessages.properties'(我正在使用Netbeans 7.0.1)

In the ValidationMessages.properties file I've got this key/value-line: 在ValidationMessages.properties文件中,我有这个键/值行:

dev.User.emailAddress=Custom invalid email message

And my view (user.xhtml) looks like this: 我的观点(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>

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? 可能问题是我没有在web.xml中注册我的属性文件?

I should switch required="true" to @NotNull in the bean too. 我也应该在bean中将required =“true”切换为@NotNull。 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 ? 正如BalusC的博客中所解释的那样?

Thanks in advance! 提前致谢!

/ Dennis /丹尼斯

I've also created a file 'ValidationMessages.properties' in WEB-INF/classes (I'm using Netbeans 7.0.1) 我还在WEB-INF / classes中创建了一个文件'ValidationMessages.properties'(我正在使用Netbeans 7.0.1)

I understand that you've put it straight in the project's /WEB-INF/classes folder yourself? 我知道你自己直接把它放在项目的/WEB-INF/classes文件夹中了吗? This will likely be ignored/overridden if you let your IDE build/deploy the WAR (at least, this is true for Eclipse). 如果让IDE构建/部署WAR,这可能会被忽略/覆盖(至少对Eclipse来说是这样)。 You should rather put that file straight in the root of the Java source folder of the project. 您应该将该文件直接放在项目的Java源文件夹的根目录中。 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. 在将项目导出到WAR文件然后使用某些ZIP工具提取之后,您可以验证自己是否真的在/WEB-INF/classes中结束。

I placed the .properties file in the resources folder and it worked, there was no need to register the file anywhere. 我将.properties文件放在resources文件夹中并且它工作正常,无需在任何地方注册该文件。 For the record: I'm using Netbeans 7.0.1 and Maven. 记录:我正在使用Netbeans 7.0.1和Maven。

Thanks for the help, BalusC! 感谢您的帮助,BalusC!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM