问题
I have an <ui:repeat>
with <ui:inputText>
:
<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>
This is the #{genproducts}
backing bean:
@ManagedBean(name = "genproducts")
@ViewScoped
public class Genproducts{
public List<Product> dbList() throws SQLException {
List<Product> list = new ArrayList<>();
...
return list;
}
}
This is the Product
entity:
@ManagedBean(name = "product")
@RequestScoped
public class Product {
private int amount;
public int getAmount() {
return amount;
}
public void setAmount(int amount) {
this.amount = amount;
}
}
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?
回答1:
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.
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;
}
}
Which is to be referenced as
<ui:repeat value="#{genproducts.list}" var="itemsBuying">
See also
- How and when should I load the model from database for h:dataTable
- Why JSF calls getters multiple times
来源:https://stackoverflow.com/questions/16909715/hinputtext-doesnt-seem-to-work-within-uirepeat-only-the-last-entry-is-su