【问题标题】:Get the validation message for a specific component获取特定组件的验证消息
【发布时间】:2023-03-19 05:01:01
【问题描述】:
<h:inputText id="myInputText"
             title="The text from validation message here"
             style="#{component.valid? '' : 'border-color:red'}"
             validator="#{MyBean.validate}"
             required="true"
             requiredMessage="required"
             value="#{MyBean.value} />
<p:message for="myInputText" display="text"/>

由于我想自定义在 inputText 组件中查找失败的验证,并且我知道可以知道组件是否已成功验证,所以我想知道它是否可行以及如何获得验证消息,以便将其显示为我的 inputText 组件的标题。

【问题讨论】:

    标签: validation jsf jakarta-ee


    【解决方案1】:

    您计划的问题是单个组件可以有多个消息排队。那你打算怎么办?出于演示目的,您可以使用

    <h:inputText id="myInputText"
                 title="#{facesContext.getMessageList('myInputText').get(0)}"
                 style="#{component.valid ? '' : 'border-color:red'}"
                 validator="#{MyBean.validate}"
                 required="true"
                 requiredMessage="required"
                 value="#{MyBean.value}" />
    

    编辑:您应该将逻辑移到您的支持 bean 中:

    1. 在给定clientId 的情况下,实现一个从可用的FacesMessage 列表中提取详细信息的方法

      public String getComponentMessageDetail(String clientId) {
          String detail = null;
          FacesContext ctxt = FacesContext.getCurrentInstance();
          List<FacesMessage> componentMessages = ctxt.getMessages(clientId);
      
          if (componentMessages != null && componentMessages.isEmpty() == false) {
              //returns the detail, from only the first message!
              detail = componentMessages.get(0).getDetail();
          }
      
          return detail;
      }
      
    2. 在你的视图中使用实用方法

       <h:inputText id="myInputText"
                    title="#{MyBean.getComponentMessageDetail('myInputText')}"
                    style="#{component.valid ? '' : 'border-color:red'}"
                    validator="#{MyBean.validate}"
                    required="true"
                    requiredMessage="required"
                    value="#{MyBean.value}" />
      

    【讨论】:

    • 感谢您的宝贵时间和回答。这种方法可以帮助我达到我想要的解决方案,但是您的演示返回的是一个 FacesMessage 对象,我想在我的视图中显示消息的详细信息/摘要,而不是它的 ID,你知道我该如何转换是吗?
    • 此时,最好将所有逻辑移至支持 bean,因为在视图 @unpix 中做这么多工作并不整洁
    • 感谢@kolossus,愚蠢的我没想到将逻辑放在支持bean中,一直在学习!顺便说一句,不是ctxt.getMessages(clientId),而是ctxt.getMessageList(clientId); 但是你知道为什么我不能从我的视图中访问 getDetail 方法吗?
    • @unpix - 你应该可以在视图中getDetail。你尝试过但失败了吗?当你尝试时会发生什么?
    • 我在我的 tomcat 日志中得到了这个:在 javax.faces.application.FacesMessage 类型上找不到属性 'getDetail'
    【解决方案2】:

    这个java方法怎么样

    public String getComponentMessageDetail(String cid){
      FacesContext ctxt  =  FacesContext.getCurrentInstance();
      Iterator<FacesMessage> cm = ctxt.getMessages(cid);
      List<String> msg = new ArrayList<>();
      while(cm.hasNext()) {
        msg.add(cm.next().getDetail());
      }
      return String.join(" | ", msg);
    }
    

    显示消息缓存中的所有内容?

    同样在 xhtml 中

    <h:inputText id="myInputText" title="#{MyBean.getComponentMessageDetail('yourFormId:myInputText'}" style="#{component.valid? '' : 'border-color:red'}" validator="#{MyBean.validate}" required="true" requiredMessage="required" value="#{MyBean.value} />
    

    将表单ID 的名称放在输入控件的ID 前面可能会很有用。否则消息列表可能有零个项目,尽管有一些。

    这是另一种快速显示验证消息的方法:h:messages

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-06-04
      • 2019-07-17
      • 1970-01-01
      • 1970-01-01
      • 2013-01-08
      • 2023-03-27
      • 2019-07-22
      • 2020-05-26
      相关资源
      最近更新 更多