JSF如何生成表单输入字段的名称? [英] How does JSF generate the name of the form input field?

查看:85
本文介绍了JSF如何生成表单输入字段的名称?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人知道吗?是否可以指定表单输入字段的名称? 该怎么做呢?

Any idea anyone? Is it possible that we specify the name of the form input field? How to go about doing that?

推荐答案

如上所述,HTML名称和id属性是由命名容器并基于应用程序名称空间生成的.当控件是重复控件的子级(如UIData)或JSP在一页中呈现两次(如在portlet环境中)时,这可以防止发生冲突.呈现给HTML的ID为 clientId .

As has been stated, the HTML name and id attributes are generated by the naming containers and based on the application namespace. This prevents collisions when controls are children of repeating controls (like a UIData) or a JSP is rendered twice in one page (like in a portlet environment). The id rendered to the HTML is the clientId.

可以手动对clientId进行硬编码或构建,但这是一种非常脆弱的方法.最好使用组件的 getClientId(FacesContext)方法;这就是渲染器使用的.

It is possible to hardcode or build the clientId manually, but this is a very fragile approach. It is better to use the component's getClientId(FacesContext) method; this is what the renderers use.

可以为绑定的组件获取clientId的bean:

A bean that can get the clientId for a bound component:

/** Request scope */
public class IdBean implements Serializable {
  private UIComponent mytext;

  public String getClientId() {
    return mytext.getClientId(FacesContext.getCurrentInstance());
  }

  public UIComponent getMytext() { return mytext; }
  public void setMytext(UIComponent mytext) { this.mytext = mytext; }

  public List<String> getRows() {
    List<String> rows = new ArrayList<String>();
    for (int i = 0; i < 10; i++) {
      rows.add("row" + i);
    }
    return rows;
  }
}

视图:

  <f:view>
    <h:form>
      <h:dataTable value="#{idBean.rows}" var="row">
        <h:column>
          <h:outputLabel value="#{row}" />
          <h:inputText binding="#{idBean.mytext}"
            onclick="foo('#{idBean.clientId}');" />
        </h:column>
      </h:dataTable>
    </h:form>
  </f:view>

  <script type="text/javascript">
    function foo(name) {
        alert('You clicked '+name);
    }
  </script>

mytext 控件被渲染了10次,因此任何发出其名称的代码也必须是dataTable的子级.

The mytext control is rendered 10 times, so any code that emits its name must also be a child of the dataTable.

这篇关于JSF如何生成表单输入字段的名称?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆