我希望用户为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映射到数组属性?
发布评论