<h:inputText>似乎在 <ui:repeat> 中不起作用,只提交最后一个条目 [英] <h:inputText> doesn't seem to work within <ui:repeat>, only the last entry is submitted

查看:37
本文介绍了<h:inputText>似乎在 <ui:repeat> 中不起作用,只提交最后一个条目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 :

<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>

</ui:repeat></ui:define></ui:composition>

这是 #{genproducts} 支持 bean:

@ManagedBean(name = "genproducts")@ViewScoped公共类 Genproducts{公开列表<产品>dbList() 抛出 SQLException {列表<产品>list = new ArrayList<>();...退货清单;}}

这是 Product 实体:

@ManagedBean(name = "product")@RequestScoped公共类产品{私人整数金额;公共 int getAmount() {返还金额;}公共无效 setAmount(int 金额) {this.amount = 金额;}}

就我而言,dbList() 方法有四种产品.对于前三个产品,当我输入不同的值时,默认值出现在操作方法中.仅对于最后一个产品,它按预期工作.

这是怎么引起的,我该如何解决?

解决方案

这是因为您在 后面的 getter 方法中(重新)创建列表.在每个迭代轮中调用此方法.因此,每次下一次迭代基本上都会丢弃上一次迭代期间设置的值.在 action 方法中,您最终会得到在上一轮迭代期间创建的列表.这就是为什么最后一个条目似乎工作正常.

这种做法确实是绝对不对的.您根本不应该在 getter 方法中执行业务逻辑.使列表成为一个属性,并在 bean 的(后)构造过程中只填充一次.

@ManagedBean(name = "genproducts")@ViewScoped公共类 Genproducts{私人列表<产品>列表;@PostConstructpublic void init() 抛出 SQLException {list = new ArrayList<>();//...}公开列表<产品>获取列表(){退货清单;}}

引用为

另见

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?

解决方案

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

这篇关于&lt;h:inputText&gt;似乎在 &lt;ui:repeat&gt; 中不起作用,只提交最后一个条目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
其他开发最新文章
热门教程
热门工具
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆