如何将多个 inputText 映射到数组属性? [英] How map multiple inputText to an array property?
问题描述
我希望用户为 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]}" />
或使用
和 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>
不要不要像
<ui:repeat value="#{myBean.names}" var="name">
<h:inputText value="#{name}" />
</ui:repeat>
提交表单时不起作用,因为 String
没有值的 setter(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屋!