2014-01-27 79 views
0

我正在使用Primefaces和微調控制器組件。我的問題是,如果它在迭代結構中,那麼微調器值不在bean中設置。我的微調在ui裏面:重複。 最後,問題在於如何處理映射到bean中相同屬性的不同表單控件。重複結構中捕捉PrimeFaces的微調器值

<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對於所有旋轉者都是不同的。

任何幫助,將不勝感激。

回答

0

System.out.println("quantity: " + quantityToOrder)放在您的setQuantityToOrder(int quantityToOrder)方法中,將會看到問題。最後一個微調器的價值將優於其他微調器,因爲所有微調器都指向相同的屬性(cartBean.quantityToOrder)。

嘗試quantityToOrder移動到項目如下:

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

} 
+0

感謝丹尼爾。你的解決方案有效訣竅究竟是從commandButton中刪除ajax =「false」。 – Carlos