代码之家  ›  专栏  ›  技术社区  ›  sblundy

确定JSF容器表单的ID

  •  3
  • sblundy  · 技术社区  · 17 年前

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

    包含.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>
    

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

    3 回复  |  直到 17 年前
        1
  •  5
  •   jsight TaherT    17 年前

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

        2
  •  0
  •   lightguard-jp    17 年前

    在编程上,我会使用jsight的方法。你可以通过查看来知道你的元素的id(除非你让JSF创建它们,否则我不知道id中编号的方法)。h:form是一个命名容器,只要你没有把它包装在另一个命名集装箱中,它就会包含form:containefield“:”是默认的命名分隔符是JSF,id大致是这样创建的(parentNamingContainerId:)*componentId

        3
  •  0
  •   rm rm    17 年前

    由于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 .........
    
    }
    
    推荐文章