【问题标题】:How map multiple inputText to an array property?如何将多个 inputText 映射到数组属性?
【发布时间】:2011-07-19 02:14:19
【问题描述】:

我希望用户在 JSF 的 inputText 组件中输入一个或多个名称。 所以我在想一个这样的托管bean:

public class MyBean {

    private String[] names;

    public String[] getNames() {
        return names;
    }

    public void setNames(String[] names) {
        this.names = names;
    }
}

但是,如何将 JSF 的 inputText 组件映射到这个数组属性?

【问题讨论】:

    标签: jsf jsf-2


    【解决方案1】:

    首先,您需要将数组保存在 bean 的(后)构造函数中。例如

    public MyBean() {
        names = new String[3];
    }
    

    然后,您可以或者通过硬编码索引访问它们

    <h:inputText value="#{myBean.names[0]}" />
    <h:inputText value="#{myBean.names[1]}" />
    <h:inputText value="#{myBean.names[2]}" />
    

    使用 &lt;ui:repeat&gt;varStatus 以通过动态索引访问它们

    <ui:repeat value="#{myBean.names}" varStatus="loop">
        <h:inputText value="#{myBean.names[loop.index]}" />
    </ui:repeat>
    

    不要不要像使用var属性一样

    <ui:repeat value="#{myBean.names}" var="name">
        <h:inputText value="#{name}" />
    </ui:repeat>
    

    提交表单时它不会起作用,因为String 没有值的setter(getter 基本上是toString() 方法)。

    【讨论】:

      【解决方案2】:

      这是我如何使用上面的例子。

      <c:forEach items="#{cotBean.form.conductor}" varStatus="numReg">
          <ice:panelGroup>
              <ice:selectOneMenu value="#{cotBean.form.conductor[numReg.index].gender}">
              </ice:selectOneMenu>
          </ice:panelGroup>
          <ice:panelGroup>
              <ice:selectOneMenu value="#{cotBean.form.conductor[numReg.index].dob.day}">
              </ice:selectOneMenu>
              <ice:selectOneMenu value="#{cotBean.form.conductor[numReg.index].dob.month}">
              </ice:selectOneMenu>
              <ice:selectOneMenu value="#{cotBean.form.conductor[numReg.index].dob.year}">
              </ice:selectOneMenu>
          </ice:panelGroup>
      </c:forEach>
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2014-03-31
        • 1970-01-01
        • 2015-01-11
        • 1970-01-01
        • 1970-01-01
        • 2020-06-27
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多