【问题标题】:Seam Hibernate Validator displaying JSF messages显示 JSF 消息的 Seam Hibernate 验证器
【发布时间】:2011-09-18 18:50:25
【问题描述】:

我有一个使用 Seam 2.2.2- JSF 1.1 - Hibernate Validator 3.1.0.GA 的应用程序。我正在尝试使用 hibernate validate message=() 选项显示自定义消息。 message=() 会显示给其他验证器,例如 @Length。但是我为@NotNull 设置的消息不起作用。

这里是代码sn-p。

<s:decorate id="decoration1" template="/includes/myTemplate.xhtml">         
 <ui:define name="label">userId</ui:define>
 <h:inputTextarea label="userIdFromTemplate" value="#myController.user.userId}" required="true" >
       <a4j:support event="onblur" reRender="decoration1" ajaxSingle="true"  bypassUpdates="true"/>
  </h:inputTextarea>
</s:decorate>

我的控制器

@Out(required = false) @Valid
Object user;

用户对象字段

 @NotNull(message="userIdFromAnno is required")
 getUserId() {
 }

当我在 messages.properties 中设置 javax.faces.component.UIInput.REQUIRED={0} 是必填字段时,我得到 userIdFromTemplate 是必填字段。否则,我会收到 JSF 默认消息验证错误:需要值。我从来没有收到“需要 UserIdFromAnno”的消息。

相同的代码工作并为@Length 提取注释消息。

有人可以帮忙吗?

【问题讨论】:

    标签: jsf seam bean-validation


    【解决方案1】:

    默认情况下,空 HTTP 请求参数作为空字符串提交。这与 null 不同,因此永远不会调用 @NotNull。在 JSF 2.0 上,您可以使用上下文参数让 JSF 将提交的空值解释为 null。在 JSF 1.2 上,您可以使用自定义字符串转换器让 JSF 将提交的空值转换为 null。然而,在 JSF 1.1 上,根据设计,永远不会调用字符串的自定义转换器。

    最好的办法是使用 Hibernate Validator 特定的 @NotBlank 注释而不是 @NotNull

    另见:

    【讨论】:

    • 我试过你在另一篇文章中概述的转换器。当我在调试中运行时,我看到该值设置为 null,然后面临 UIInput 的验证。 @NotNull Hibernate Validator 没有被调用。
    • 如我的回答中所述,转换器不适用于 JSF 1.1。使用@NotBlank
    猜你喜欢
    • 2012-09-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-05-28
    • 1970-01-01
    • 2012-07-29
    • 1970-01-01
    相关资源
    最近更新 更多