简体   繁体   English

<h:inputText> 似乎不起作用 <ui:repeat> ,仅提交最后一个条目

[英]<h:inputText> doesn't seem to work within <ui:repeat>, only the last entry is submitted

I have an <ui:repeat> with <ui:inputText> : 我有一个<ui:repeat><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: 这是#{genproducts}支持豆:

@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: 这是Product实体:

@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. 就我而言, dbList()方法有四个产品。 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? 这是怎么引起的,我该如何解决?

It's caused because you're (re)creating the list in the getter method behind <ui:repeat value> . 这是因为您正在( <ui:repeat value><ui:repeat value>后面的getter方法中创建列表。 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. 在action方法中,您最终得到的是在上一轮迭代中创建的列表。 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. 您根本不应该在getter方法中执行业务逻辑。 Make the list a property and fill it only once during bean's (post)construction. 使列表成为属性,并在bean的(后)构造过程中仅填充一次。

@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 也可以看看

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM