确定JSF容器表单的ID
时间:2020-03-06 14:37:10 来源:igfitidea点击:
我需要从操作处理程序中确定表单字段的ID。该字段是所包含的facelets组件的一部分,因此形式会有所不同。
included.xhtml
<ui:component> <h:inputText id="contained_field"/> <h:commandButton actionListener="#{backingBean.update}" value="Submit"/> </ui:component>
example_ contains.xhtml
<h:form id="containing_form"> <ui:include src="/included.xhtml"/> </h:form>
在运行时,如何在update方法中确定表单的ID?或者更好的是,直接输入字段的ID。
解决方案
将按钮绑定到支持bean,然后使用getParent()直到找到最接近的表单。
我会以编程方式使用jsight的方法。通过查看可以知道元素的ID(除非让JSF创建它们,否则我不知道编号ID的方法)。 h:form是一个命名容器,只要我们没有将其包装在另一个命名容器中,它将包含Form:containedfield默认情况下,":"是命名分隔符是JSF,并且ID都是这样创建的,(parentNamingContainerId:)* componentId
由于update方法的类型为actionListener,因此我们可以按以下方式访问UI组件
public void update(javax.faces.event.ActionEvent ac) { javax.faces.component.UIComponent myCommand = ac.getComponent( ); String id = myCommand.getId(); // get the id of the firing component ..... your code ......... }