15

是否可以更新我的页面中不是 JSF 组件的部分?

例如,我可以更新纯 HTML<div>还是需要将其包装在 JSF 组件中?

4

3 回答 3

33

Is it possible to update parts of my page that are not JSF components?

No. The to-be-updated component has to be available by UIViewRoot#findComponent(), so that JSF can find them, invoke encodeAll() on it, capture the generated HTML output and pass back it in the ajax response so that JavaScript can update the HTML DOM tree with it. Plain HTML elements are not represented as real UIComponent instances in the JSF component tree, so JSF already cannot locate them in first place.


For example, can I update a plain HTML <div> or do I need to wrap that in a JSF component?

You need to wrap it in a JSF component like <h:panelGroup>. You can however just use <h:panelGroup layout="block"> to represent a real <div> in JSF. This way you don't need to wrap the <div> in another JSF component.

<h:panelGroup layout="block" id="foo">
    ...
</h:panelGroup>

Since JSF 2.2 you can use new passthrough elements feature with jsf:id attribute to declare HTML(5) elements as JSF components.

<... xmlns:jsf="http://xmlns.jcp.org/jsf">

<div jsf:id="foo">
    ...
</div>
<main jsf:id="bar">
    ...
</main>
<section jsf:id="baz">
    ...
</section>

They will render their output as-is, but under the covers be a concrete UIPanel instance.

There's however one corner case in case of composite components. You can use the following approach to have a HTML element which is updateable by ajax.

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

The explanation that this approach works is because even though the composite component does not render itself to the HTML output, it is by itself available by UIViewRoot#findComponent().

See also:

于 2012-10-22T12:55:48.467 回答
1

看来你不能。

要更新某些东西,请将其包装在“可更新”组件中(在 primeaces p:outputpanel 中)并更新该组件。

另外:在您的特殊情况下,您可以通过这种方式刷新 p:tree 的子级:JSF update primefaces tree children

(哈哈一直想用第三人称跟我说话)

于 2012-10-22T11:48:42.307 回答
-2

AFAIK 你通过它们的 ID 更新组件,因为常规元素可以有一个 ID,只要你得到正确的 ID 就应该是可能的;-)

于 2012-10-22T10:14:09.707 回答