简体   繁体   English

p:ajax使用全局HTML ID来引用 <h:messages> 属性

[英]p:ajax using an global HTML id to reference to an <h:messages> attribute

I currently working on an project that needs some "rebuild" but still there is no money on this - so I have to deal with that what's in front of me. 我目前正在从事一个需要“重建”的项目,但仍然没有钱可做-因此我必须处理摆在我面前的问题。 I need to display an message on every change of the radioButton as you can see in this code example: 如下面的代码示例所示,我需要在radioButton的每次更改上显示一条消息:

<h:form>
    <h:messages id="msg"/>
    <p:accordionPanel value="#{exerciseBean.getCategoriesInGrade(5)}" var="category">
        <p:tab title="#{category.categoryName}">
            <h:panelGrid columns="1" cellpadding="5"> 
                <ui:repeat value="#{exerciseBean.getSingleChoiceInGrade(5, category.categoryName)}" var="exercise">
                     <div>#{exercise.getText()}</div>
                     <p:selectOneRadio value="#{exerciseBean.data}" > 
                         <f:selectItems value="#{exercise.getChoices()}" var="c" itemLabel="#{c.id}" itemValue="#{c.id}"  />  
                         <p:ajax event="change" process="@this" listener="#{exerciseBean.validateInput(exercise.getId())}" update="msg"/>
                      </p:selectOneRadio>  
                </ui:repeat>
            </h:panelGrid>
        </p:tab>
     </p:accordionPanel>  
</h:form>

As you can see - inside this <h:form> there is directly an <h:messages> with the id msg . 如您所见-在<h:form>中直接存在一个ID为msg<h:messages> Now the problem: I still get the error: "cannot find component with identifier 'msg'". 现在的问题是:我仍然收到错误:“找不到标识符为'msg'的组件”。 I now tried it with the prefix : but still the same problem. 我现在尝试使用前缀:但是仍然是同样的问题。 Is there a way to get PrimeFaces/JSF to reference to this "global-id"? 有没有办法让PrimeFaces / JSF引用此“ global-id”?

In my backend I send the message like this: 我在后端发送如下消息:

FacesMessage facesMessage = new FacesMessage("Right Answer!");
FacesContext.getCurrentInstance().addMessage(null, facesMessage);

thanks. 谢谢。

  1. You need an id for your form. 您需要表单的ID。
  2. You then need to modify the reference to msg in the ajax update attribute. 然后,您需要在ajax update属性中修改对msg的引用。

So the following code change should do it for you. 因此,以下代码更改应为您完成。

1. <h:form id="formID" > 1. <h:form id="formID" >

2. <p:ajax event="change" process="@this" listener="#{exerciseBean.validateInput(exercise.getId())}" update=":formID:msg"/> 2. <p:ajax event="change" process="@this" listener="#{exerciseBean.validateInput(exercise.getId())}" update=":formID:msg"/>

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

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