如何收集 List的提交值在JSF?
我有一个带有 List
的 bean:
@Named
@ViewScoped
public class Bean {
private List<Item> items;
private String value;
@Inject
private ItemService itemService;
@PostConstruct
public void init() {
items = itemService.list();
}
public void submit() {
System.out.println("Submitted value: " + value);
}
public List<Item> getItems() {
return items;
}
}
我想编辑每个项目的 value
属性:
<h:form>
<ui:repeat value="#{bean.items}" var="item">
<h:inputText value="#{bean.value}" />
</ui:repeat>
<h:commandButton action="#{bean.submit}" />
</h:form>
使用此代码,value
> 不包含所有提交的值,仅包含最新提交的值。我还尝试了
和
,但没有任何区别。
我应该如何收集所有提交的值?
I have a bean with a List<T>
:
@Named
@ViewScoped
public class Bean {
private List<Item> items;
private String value;
@Inject
private ItemService itemService;
@PostConstruct
public void init() {
items = itemService.list();
}
public void submit() {
System.out.println("Submitted value: " + value);
}
public List<Item> getItems() {
return items;
}
}
And I'd like to edit the value
property of every item:
<h:form>
<ui:repeat value="#{bean.items}" var="item">
<h:inputText value="#{bean.value}" />
</ui:repeat>
<h:commandButton action="#{bean.submit}" />
</h:form>
With this code the value
doesn't contain all the submitted values, only the latest submitted value. I also tried <c:forEach>
and <h:dataTable>
, but it didn't made any difference.
What should I do for collecting all the submitted values?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的问题是由于您基本上将所有提交的值收集到同一个 bean 属性中而引起的。您需要将
value
属性移至var="item"
后面的 bean 中。在 bean 操作方法中,只需迭代
items
即可通过item.getValue()
获取所有提交的值。Your problem is caused because you're basically collecting all submitted values into one and same bean property. You need to move the
value
property into the bean behindvar="item"
.In the bean action method, simply iterate over
items
in order to get all submitted values viaitem.getValue()
.