如何将用户键入的值放入字符串列表?
How to put user-typed values into a List of Strings?
我当前的 JSF 版本是 2.1,我想找到一个简短的解决方案,将用户输入的 String
放入单个 List
。在托管 bean 中:
@ManagedBean
@SessionScoped
public class MyBean{
private List<String> values;
public void List<Integer> getIdentifiers(){
List<Integer> lst = new ArrayList<Integer>();
//...
return lst;
}
//GET, SET, OTHER STUFF
}
和标记:
<c:forEach items="#{myBean.identifiers}" var="id">
<h:inputText value="__WHAT?__" id="input#{id}" />
</c:forEach>
我需要将用户输入的每个值放入 inputText
s 到 values
List<String>
中。我该怎么做,或者根本不可能吗?
UPD:我需要用 <c:forEach>
循环来做到这一点,因为我可能应该添加一些其他输入字段,例如 rich:calendar
,它们将按如下方式呈现:
<c:forEach items="#{myBean.identifiers}" var="id">
<c:choose>
<c:when test="__calendar_needed__">
<rich:calendar />
</c:when>
<c:when test="__input_needed__">
<h:inputText value="__WHAT?__" id="input#{id}" />
</c:when>
</c:choose>
</c:forEach>
由于您使用的是 JSF 2,因此可以使用 <ui:repeat>
tag as a replacement for <c:forEach>
(because mixing JSTL with JSF can lead to strange behavior)。
也可以直接设置value
属性指向bean中的values
集合(当然要提供相应的访问器)。集合中的每个对象将被称为 item
:
<ui:repeat value="#{myBean.values}" var="item">
<h:inputText value="#{item}" />
</ui:repeat>
我当前的 JSF 版本是 2.1,我想找到一个简短的解决方案,将用户输入的 String
放入单个 List
。在托管 bean 中:
@ManagedBean
@SessionScoped
public class MyBean{
private List<String> values;
public void List<Integer> getIdentifiers(){
List<Integer> lst = new ArrayList<Integer>();
//...
return lst;
}
//GET, SET, OTHER STUFF
}
和标记:
<c:forEach items="#{myBean.identifiers}" var="id">
<h:inputText value="__WHAT?__" id="input#{id}" />
</c:forEach>
我需要将用户输入的每个值放入 inputText
s 到 values
List<String>
中。我该怎么做,或者根本不可能吗?
UPD:我需要用 <c:forEach>
循环来做到这一点,因为我可能应该添加一些其他输入字段,例如 rich:calendar
,它们将按如下方式呈现:
<c:forEach items="#{myBean.identifiers}" var="id">
<c:choose>
<c:when test="__calendar_needed__">
<rich:calendar />
</c:when>
<c:when test="__input_needed__">
<h:inputText value="__WHAT?__" id="input#{id}" />
</c:when>
</c:choose>
</c:forEach>
由于您使用的是 JSF 2,因此可以使用 <ui:repeat>
tag as a replacement for <c:forEach>
(because mixing JSTL with JSF can lead to strange behavior)。
也可以直接设置value
属性指向bean中的values
集合(当然要提供相应的访问器)。集合中的每个对象将被称为 item
:
<ui:repeat value="#{myBean.values}" var="item">
<h:inputText value="#{item}" />
</ui:repeat>