是否可以使用 JSF ajax 更新非 JSF 组件(纯 HTML)? [英] Is it possible to update non-JSF components (plain HTML) with JSF ajax?

查看:30
本文介绍了是否可以使用 JSF ajax 更新非 JSF 组件(纯 HTML)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

例如,我可以更新纯 HTML

还是需要将其包装在 JSF 组件中?

解决方案

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

没有.要更新的组件必须通过 UIViewRoot#findComponent(),让JSF可以找到它们,在上面调用encodeAll(),捕获生成的HTML输出并在 ajax 响应中传回它,以便 JavaScript 可以用它更新 HTML DOM 树.纯 HTML 元素在 JSF 组件树中并不表示为真正的 UIComponent 实例,因此 JSF 已经无法首先定位它们.


<块引用>

例如,我可以更新纯 HTML

还是需要将其包装在 JSF 组件中?

您需要将其包装在一个 JSF 组件中,例如 .但是,您可以仅使用 来表示 JSF 中真正的

.这样您就不需要将

包装在另一个 JSF 组件中.

...</h:panelGroup>

从 JSF 2.2 开始,您可以使用新的 passthrough 元素 功能使用 jsf:id 属性将 HTML(5) 元素声明为 JSF 组件.

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

...</main>

...</节>

他们将按原样呈现他们的输出,但在幕后是一个具体的 UIPanel 实例.

然而,对于复合组件,有一种极端情况.您可以使用以下方法来创建一个 可通过 ajax 更新的 HTML 元素.

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

这种方法有效的解释是,即使复合组件将自身呈现给 HTML 输出,它本身可以通过 UIViewRoot# 获得findComponent().

另见:

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

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

解决方案

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:

这篇关于是否可以使用 JSF ajax 更新非 JSF 组件(纯 HTML)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆