确定JSF容器表单的ID

时间:2021-01-16 19:59:17

I need to determine the ID of a form field from within an action handler. The field is a part of a included facelets component and so the form will vary.

我需要从动作处理程序中确定表单字段的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>

How may I determine the ID of the form in the update method at runtime? Or better yet, the ID of the input field directly.

如何在运行时在update方法中确定表单的ID?或者更好的是,直接输入字段的ID。

3 个解决方案

#1


5  

Bind the button to your backing bean, then use getParent() until you find the nearest form.

将按钮绑定到您的支持bean,然后使用getParent()直到找到最近的表单。

#2


0  

Programmatically I would use jsight's method. You can know the id of your elements (unless you let JSF create them, I don't know the means for numbering in the ids) by looking at it. h:form is a naming container so as long as you don't have it wrapped in another naming container it will be containingForm:containedfield The ':' is the naming separator by default is JSF and the ids are created like this, roughly anyway, (parentNamingContainerId:)*componentId

我会以编程方式使用jsight的方法。您可以通过查看它来了解元素的ID(除非您让JSF创建它们,我不知道在ID中编号的方法)。 h:form是一个命名容器,所以只要你没有把它包装在另一个命名容器中它就会包含Form:containsfield':'是默认的命名分隔符是JSF而且这样的id是这样创建的,大致无论如何,(parentNamingContainerId:)* componentId

#3


0  

Since update method is of type actionListener, you can access your UI component as follows

由于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 .........

}

#1


5  

Bind the button to your backing bean, then use getParent() until you find the nearest form.

将按钮绑定到您的支持bean,然后使用getParent()直到找到最近的表单。

#2


0  

Programmatically I would use jsight's method. You can know the id of your elements (unless you let JSF create them, I don't know the means for numbering in the ids) by looking at it. h:form is a naming container so as long as you don't have it wrapped in another naming container it will be containingForm:containedfield The ':' is the naming separator by default is JSF and the ids are created like this, roughly anyway, (parentNamingContainerId:)*componentId

我会以编程方式使用jsight的方法。您可以通过查看它来了解元素的ID(除非您让JSF创建它们,我不知道在ID中编号的方法)。 h:form是一个命名容器,所以只要你没有把它包装在另一个命名容器中它就会包含Form:containsfield':'是默认的命名分隔符是JSF而且这样的id是这样创建的,大致无论如何,(parentNamingContainerId:)* componentId

#3


0  

Since update method is of type actionListener, you can access your UI component as follows

由于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 .........

}