我需要在动作处理程序中确定表单字段的ID。该字段是包含的facelets组件的一部分,因此表单将有所不同。

included.xhtml

<ui:component>
  <h:inputText id="contained_field"/>
  <h:commandButton actionListener="#{backingBean.update}" value="Submit"/>
</ui:component>

<强> example_containing.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:containsfield':'是默认的命名分隔符是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 .........

}
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top