代码之家  ›  专栏  ›  技术社区  ›  Benas

JSF2使用ajax更新复合元素

  •  3
  • Benas  · 技术社区  · 13 年前

    有一个输入元素,我想在用户在其中输入内容后对其进行更新。

    输入元素:

    <h:form id="form">
        <h:inputText value="#{message.message}"> 
            <f:ajax event="keyup" render="form:compElem"/> 
        </h:inputText> 
        <compositeOutputComponents:test2 id="compElem" message="#{message.message}"/>
    </h:form>
    

    这就是我的复合元素的样子:

    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:h="http://java.sun.com/jsf/html"
      xmlns:ui="http://java.sun.com/jsf/facelets"
      xmlns:f="http://java.sun.com/jsf/core"
      xmlns:composite="http://java.sun.com/jsf/composite">
      <h:head>
      </h:head>
        <h:body>
                <fieldset>
                    <composite:interface>
                        <composite:attribute name="id"/>
                        <composite:attribute name="message" required="true"/>
                    </composite:interface>
                    <composite:implementation>
                        <span id="#{cc.attrs.id}">
                            <h:outputText value="#{cc.attrs.message}"/>
                        </span>
                    </composite:implementation>
                </fieldset>
        </h:body>
    

    我试图将id作为Balus传递给复合元素 JSF Updating Composite Component (Primefaces) 建议(我不使用primefaces),但页面仍然产生错误:“malformedXML:在更新过程中:找不到form:compElem”。当我使用非复合元素时,一切都很好。

    1 回复  |  直到 9 年前
        1
  •  3
  •   Community Mohan Dere    9 年前

    你需要 #{cc.clientId} 而不是 #{cc.attrs.id} 在复合实现中。

    <span id="#{cc.clientId}">
    

    否则,它将以 <span id="compElem"> 而不是 <span id="form:compElem"> 然后JavaScript再也找不到它了,导致了这个错误的XML错误。

    一旦你修复了那个部分,你就可以在中引用它 <f:ajax render> 通常的方式,就像您引用任何其他形式相同的非复合组件一样:

    <f:ajax ... render="compElem" />
    

    或者,如果您出于某种原因坚持指定绝对客户端ID(但这是不必要的):

    <f:ajax ... render=":form:compElem" />
    

    另请参阅: