【问题标题】:getting component id with colon by componentgetClientId(context)通过 componentgetClientId(context) 获取带有冒号的组件 id
【发布时间】:2014-12-13 21:04:10
【问题描述】:

我使用的是 PF 5.0 和 jsf 2.2.8,下面有代码。

<ui:composition template="/template/mastertemplate.xhtml">
<ui:define name="content">
<f:view>
   <h:body>
    <h:form id="bcr_form_2" styleClass="form">
    <p:tabView id="bcrId_tabview">
    <p:tab title="Budget Cash Demands">
    <h:panelGrid>

<p:commandButton value="save" id="saveDemandId_cmdbutton"
action="#{budgetControlRegisterAction.saveNewDemand()}"
binding="#{budgetControlRegisterAction.myMessageCId}"
update=":#{p:component('saveId_message')}">
</p:commandButton>
<p:message id="saveId_message" for="saveDemandId_cmdbutton" ></p:message>

</h:panelGrid>
</h:form >
<!-- and all closing tags-->

BudgetControlRegisterAction.java 类 (@ResquestScoped)

    private UIComponent myMessageCId;
    // setter and getter 


    public String saveNewDemand() {
    FacesContext context = FacesContext.getCurrentInstance();
    log.info("client Id :" + myMessageCId.getClientId(context));
    //log output is -----> client Id :bcr_form_2:bcrId_tabview:saveDemandId_cmdbutton
    //id on browser is ------>  bcr_form_2:bcrId_tabview:saveDemandId_cmdbutton

/*this is not working */    
    //when i use myMessageCId.getClientId(context) nothing displayed on client side
    FacesContext.getCurrentInstance().addMessage(myMessageCId.getClientId(context),new FacesMessage(FacesMessage.SEVERITY_INFO, "some msg" , "some msg"));


/*this is working and displaying msg to client browser*/

    // but when i put hard coded component client Id (copied from web browser) e.g bcr_form_2:bcrId_tabview:saveDemandId_cmdbutton
    // see it is without leading colon (:)
    // and myMessageCId.getClientId(context) gives me id with colon(:) see log output in above lines
    FacesContext.getCurrentInstance().addMessage("bcr_form_2:bcrId_tabview:saveDemandId_cmdbutton",new FacesMessage(FacesMessage.SEVERITY_INFO, "some msg" , "some msg"));

    }

如何通过 myMessageCId.getClientId(context) 方法获取客户端 ID(不带冒号)?

【问题讨论】:

    标签: jsf-2 primefaces jsf-2.2


    【解决方案1】:

    我刚刚尝试了您的代码(我评论了您的第二个 addMessage,添加了 @RequestScoped 并删除了 composition),它在 Primefaces 5.1 和 Mojarra 2.2.8 上运行良好! 我注意到您的日志输出是:

    client Id :bcr_form_2:bcrId_tabview:saveDemandId_cmdbutton
    

    但注意日志行代码:

    log.info("client Id :" + myMessageCId.getClientId(context));
    

    前导分号已从您的字符串连接中引入。所以真正的输出是:

    bcr_form_2:bcrId_tabview:saveDemandId_cmdbutton
    

    如上所述,它有效。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-11-13
      • 1970-01-01
      • 1970-01-01
      • 2012-02-23
      • 2019-07-26
      相关资源
      最近更新 更多