是否可以使用 JSF ajax 更新非 JSF 组件(纯 HTML)? [英] Is it possible to update non-JSF components (plain HTML) with JSF ajax?
问题描述
是否可以更新页面中非 JSF 组件的部分?
例如,我可以更新纯 HTML
是否可以更新页面中非 JSF 组件的部分?
没有.要更新的组件必须通过 UIViewRoot#findComponent()
,让JSF可以找到它们,在上面调用encodeAll()
,捕获生成的HTML输出并在 ajax 响应中传回它,以便 JavaScript 可以用它更新 HTML DOM 树.纯 HTML 元素在 JSF 组件树中并不表示为真正的 UIComponent
实例,因此 JSF 已经无法首先定位它们.
<块引用>
例如,我可以更新纯 HTML
您需要将其包装在一个 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()
.
另见:
- 如何找出ajax更新/渲染组件的客户端ID?找不到带有表达式foo"的组件引用自bar"
- 为什么我需要嵌套一个带有渲染=#{some}"的组件?在另一个组件中,当我想对其进行 ajax 更新时?
- 通过ajax重新渲染复合组件
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:
- How to find out client ID of component for ajax update/render? Cannot find component with expression "foo" referenced from "bar"
- Why do I need to nest a component with rendered="#{some}" in another component when I want to ajax-update it?
- Rerendering composite component by ajax
这篇关于是否可以使用 JSF ajax 更新非 JSF 组件(纯 HTML)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!