在重复结构中捕获PrimeFaces的微调值

时间:2014-01-27 08:55:47

标签: jsf primefaces spinner

我正在使用Primefaces和spinner组件。我的问题是,如果它在迭代结构中,则不在bean中设置微调器值。我的旋转器在ui里面:重复。 最后,问题是如何处理映射到bean中相同属性的不同表单控件。

<h:form>
    <ui:repeat var="item" value="#{myBean.items}">   
        <p:spinner size="2" min="1" max="50" style="width:75px" value="#{cartBean.quantityToOrder}"/>
        <p:commandButton value="Add to cart" action="#{cartBean.saveItemToCart(item)}" ajax="false"/>
    </ui:repeat>
</h:form>    

和我的豆子

@ManagedBean
@SessionScoped
public class CartBean extends BaseBean {

    private int quantityToOrder;

   //setter, getter...

   //When called quantityToOrder = 0 always
   public void saveItemToOrder(Item item) {
       quantityToOrder IS 0.
   }
}

我怀疑它与表单提交有关,我已经尝试了一个表单,其中包含集合中的所有元素以及包含任何微调器+按钮的表单。生成的客户端ID对于所有微调器都是不同的。

任何帮助都将不胜感激。

1 个答案:

答案 0 :(得分:0)

System.out.println("quantity: " + quantityToOrder)方法上加setQuantityToOrder(int quantityToOrder),我们会看到问题所在。最后一个微调器的值优先于其他微调器,因为所有微调器都指向相同的属性(cartBean.quantityToOrder)。

尝试将quantityToOrder移动到Item,如下所示:

<h:form id="mainForm">
    <ui:repeat value="#{cartBean.items}" var="item">
        <p:outputLabel value="#{item.name}: " for="sp" />
        <p:spinner id="sp" size="2" min="1" max="50" style="width:75px" value="#{item.quantityToOrder}" />
        <p:commandButton value="Add to cart" action="#{cartBean.saveItemToOrder(item)}" process="@this, sp" update=":mainForm:total" />
        <br />
    </ui:repeat>
    Total: <h:outputText id="total" value="#{cartBean.quantityToOrder}" />
</h:form>

cartBean:

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import javax.annotation.PostConstruct;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;

@ManagedBean
@SessionScoped
public class CartBean implements Serializable {

    private List<CartItem> items;

    private int quantityToOrder;

    @PostConstruct
    public void setup() {
        items = new ArrayList<CartItem>();
        items.add(new CartItem(1, "A"));
        items.add(new CartItem(2, "B"));
        items.add(new CartItem(3, "C"));
    }

    public void saveItemToOrder(CartItem item) {
        //do whatever you want to do with the item quantity.
        System.out.println("Qtd of " + item.getName() + ": " + item.getQuantityToOrder());

        //to calculte the qtd of items on the cart.
        quantityToOrder = 0;
        for (CartItem cartItem : items) {
            quantityToOrder += cartItem.getQuantityToOrder();
        }
    }

    public List<CartItem> getItems() {
        return items;
    }

    public void setItems(List<CartItem> items) {
        this.items = items;
    }

    public int getQuantityToOrder() {
        return quantityToOrder;
    }

    public void setQuantityToOrder(int quantityToOrder) {
        this.quantityToOrder = quantityToOrder;
    }

}

CartItem:

import java.io.Serializable;

public class CartItem implements Serializable {

    private Integer id;

    private Integer quantityToOrder;

    private String name;

    public CartItem(Integer id, String name) {
        this.id = id;
        this.name = name;
        quantityToOrder = 0;
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Integer getQuantityToOrder() {
        return quantityToOrder;
    }

    public void setQuantityToOrder(Integer quantityToOrder) {
        this.quantityToOrder = quantityToOrder;
    }

    @Override
    public int hashCode() {
        int hash = 7;
        hash = 67 * hash + (this.id != null ? this.id.hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object obj) {
        if (obj == null) {
            return false;
        }
        if (getClass() != obj.getClass()) {
            return false;
        }
        final CartItem other = (CartItem) obj;
        if (this.id != other.id && (this.id == null || !this.id.equals(other.id))) {
            return false;
        }
        return true;
    }

}