4

誰かアイデアはありますか?フォーム入力フィールドの名前を指定することはできますか? どうやってそれを行うのですか?

4

4 に答える 4

5

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.

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.

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;
  }
}

The view:

  <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>

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

于 2008-11-05T15:50:37.463 に答える
5

より一般的には、すべての JSF コンポーネントには ID があります。ID を指定しない場合、JSF は j _idXXX の形式を使用して自動 ID を作成します ( XXXは増分番号です)。

一部のコンポーネントは、特に javax.faces.component.NamingContainer インターフェースを実装します<h:form>。これは、このコンポーネントのすべての子の ID が、このコンテナーの ID の前に「:」で区切られたものになることを意味します。したがって、例では:

<h:form id="myForm">
    <h:inputText id="myInput" .../>
</h:form>

入力の実際のID (つまり、HTML 入力オブジェクトの ID) はmyForm:myInputになります。

于 2008-11-05T14:09:49.757 に答える
3

formId:fieldId として生成されます

したがって、次の場合:

<h:form id="searchForm">
   <h:inputText id="searchField" ... />
</h:form>

検索フィールドの名前 (および HTML ID) は次のようになります。

searchForm:searchField

于 2008-11-05T14:01:31.007 に答える
0

ビュー、フォーム、およびいくつかのコンポーネントを含むほとんどのページでは、clientID はフォーム ID とコンポーネント ID を含むコロンで区切られた文字列になります。例:

入力テキストのクライアント ID は「myForm:myInputText」になります。サブビュー内にネストした場合、それがリストの最初のものになります。次に例を示します。

入力テキスト クライアント ID は "mySubview:myForm:myInputText" になります。

于 2008-11-05T20:52:44.063 に答える