JSF 2.0,Bean验证和标签 [英] JSF 2.0, Bean Validation and labels

查看:105
本文介绍了JSF 2.0,Bean验证和标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用bean验证时,如何在JSF错误消息中显示字段标签?

How can I show the field label in a JSF error message when bean validation is used?

<h:messages/>
<h:inputText label="Username" value="#{myBean.username}" />
...   

 

public class MyBean {
    @NotNull
    private String username;
    ...
}

如果未提交用户名,则仅显示不得为空",并且没有字段引用.

If no user name is submitted only "Must not be null" is shown and there is no field reference.

推荐答案

要在错误消息中显示相关组件的标签,请在应用程序的资源包中为属性javax.faces.validator.BeanValidator.MESSAGE定义一个新值.像这样:

To display the concerned component's label in the error message define a new value for the property javax.faces.validator.BeanValidator.MESSAGE in a resource bundle of the application like this:

javax.faces.validator.BeanValidator.MESSAGE={1}: {0}

占位符{0}表示由Bean验证运行时创建的错误消息,{1}表示组件标签.可以在 JSF 2规范中找到更多信息. 第3.5.6.3节.

The placeholder {0} refers to the error message as created by the Bean Validation runtime, {1} refers to the component label. More information can be found in the JSF 2 spec. section 3.5.6.3.

这篇关于JSF 2.0,Bean验证和标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆