如何将多个inputText映射到数组属性? [英] How map multiple inputText to an array property?

查看:81
本文介绍了如何将多个inputText映射到数组属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I want the user to enter one or more names to the JSF's inputText components. So I'm thinking of a managed bean like this:

public class MyBean {

    private String[] names;

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

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

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

But, how do I map the JSF's inputText components to this array property?

推荐答案

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

First, you need to preserve the array in bean's (post)constructor. E.g.

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

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

Then, you can either just access them by an hardcoded index

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

<ui:repeat>varStatus一起使用以通过动态索引对其进行访问

or use <ui:repeat> with a varStatus to access them by a dynamic index

<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没有值的设置方法(getter基本上是toString()方法).

It won't work when you submit the form, because String doesn't have a setter for the value (the getter is basically the toString() method).

这篇关于如何将多个inputText映射到数组属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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