&LT; H:inputText的&GT;似乎并没有在&LT工作; UI:重复&gt;中只有最后一项被提交 [英] <h:inputText> doesn't seem to work within <ui:repeat>, only the last entry is submitted
问题描述
我有一个&LT; UI:重复&GT;
与&LT; UI:inputText的&GT;
:
<ui:composition xmlns="http://www.w3.org/1999/xhtml"
xmlns:ui="http://java.sun.com/jsf/facelets"
template="./templates/masterLayout.xhtml"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:p="http://primefaces.org/ui"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:c="http://java.sun.com/jsp/jstl/core">
<ui:define name="content">
<ui:repeat value="#{genproducts.dbList()}" var="itemsBuying">
<div class="indproduct">
<p class="center">#{itemsBuying.name}</p>
<div class="center">
<h:form style="margin-left: auto; margin-right: auto;">
<h:inputText value="#{itemsBuying.amount}" />
<h:commandLink action="#{shoppingCart.addToCart(itemsBuying)}" value="add" />
</h:form>
</div>
</div>
</ui:repeat>
</ui:define>
</ui:composition>
这是#{genproducts}
支持bean:
@ManagedBean(name = "genproducts")
@ViewScoped
public class Genproducts{
public List<Product> dbList() throws SQLException {
List<Product> list = new ArrayList<>();
...
return list;
}
}
这是产品
实体:
@ManagedBean(name = "product")
@RequestScoped
public class Product {
private int amount;
public int getAmount() {
return amount;
}
public void setAmount(int amount) {
this.amount = amount;
}
}
在我的情况下,有从四款产品DBLIST()
方法。对于前三种产品,当我输入一个不同的值,默认值将出现在操作方法。仅在过去的产品,它按预期工作。
In my case, there are four products from dbList()
method. For the first three products, when I input a different value, the default value appears in action method. Only for the last product, it works as expected.
这是怎么造成的,我该怎么解决呢?
How is this caused and how can I solve it?
推荐答案
这是因为你(重新)创建落后于getter方法列表引起&LT; UI:重复值&GT;
。这种方法每次迭代轮中被调用。所以,每一个迭代基本上会垃圾桶previous迭代过程中设置的值。在操作方法,你结束了最后一轮的迭代期间创建的列表。这就是为什么最后一个条目似乎做工精细。
It's caused because you're (re)creating the list in the getter method behind <ui:repeat value>
. This method is invoked during every iteration round. So, every next iteration will basically trash the values set during the previous iteration. In the action method, you end up with the list as created during the last iteration round. That's why the last entry seems to work fine.
这方法确实是绝对不正确的。你不应该在getter方法都执行业务逻辑。使列表的属性和bean的(岗位)施工期间填补它只有一次。
This approach is indeed absolutely not right. You should not be performing business logic in getter methods at all. Make the list a property and fill it only once during bean's (post)construction.
@ManagedBean(name = "genproducts")
@ViewScoped
public class Genproducts{
private List<Product> list;
@PostConstruct
public void init() throws SQLException {
list = new ArrayList<>();
// ...
}
public List<Product> getList() {
return list;
}
}
这是因为
<ui:repeat value="#{genproducts.list}" var="itemsBuying">
另请参见
- When我应该从数据库中加载的集合H:dataTable的
- 为什么JSF调用干将多次
- When should I load the collection from database for h:dataTable
- Why JSF calls getters multiple times
See also
这篇关于&LT; H:inputText的&GT;似乎并没有在&LT工作; UI:重复&gt;中只有最后一项被提交的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!