JSF: 获取 HTML 个存在 JSF 代码的元素 ID

JSF: Get HTML element ID in which JSF code exists

想问问大家有没有办法动态获取JSF代码所在的元素ID

我的意思是fx:

    <p id="paragraph1" class="textToEdit">#{paragraphBean.getParagraphTextById("paragraph1")}</p>

在这行代码中,而不是我在 JSF 代码中编写 "paragraph1",我希望它从 <p> ID 元素中提取。

提前感谢您的所有回复..

这仅适用于 JSF 组件,不适用于普通 HTML 元素。 JSF 将在 EL 范围内将“current component”推送为 #{component}

<h:someComponent id="foo">#{bean.foo(component.id)}</h:someComponent>

最好的办法是将样板文件重构为 custom tagfile

<my:p id="paragraph1" styleClass="textToEdit" />

其中/WEB-INF/tags/p.xhtml实现如下。

<p id="#{id}" class="#{styleClass}">#{paragraphBean.getParagraphTextById(id)}</p>