I have a <h:dataTable>
displaying a product catalog in Proudcts.xhtml
:
<h:form name="ViewProductsManagedBean">
<h:dataTable var="product" value="#{ViewProductsManagedBean.productsList}">
<h:column>
<h:outputText value="#{product.productid}" />
</h:column>
<h:column>
<h:outputText value="#{product.itemcode}" />
</h:column>
<h:column>
<h:outputText value="#{product.itemdescription}" />
</h:column>
<h:column>
<h:outputText value="#{product.unitprice}" />
</h:column>
<h:column>
<h:selectOneMenu value="#{ViewProductsManagedBean.quantityPurchased}" required="true">
<f:selectItem itemValue="1" itemLabel="1" />
<f:selectItem itemValue="2" itemLabel="2" />
<f:selectItem itemValue="3" itemLabel="3" />
<f:selectItem itemValue="4" itemLabel="4" />
<f:selectItem itemValue="5" itemLabel="5"/>
</h:selectOneMenu>
</h:column>
<h:column>
<h:commandButton action="#{ViewProductsManagedBean.addItemToCart(product)}" value="Add to basket" />
</h:column>
</h:dataTable>
</h:form>
With this managed bean:
@ManagedBean(name="ViewProductsManagedBean")
@SessionScoped
public class ViewProductsManagedBean {
private double unitprice;
private String itemdescription;
private String itemcode;
private int quantityPurchased;
private String result;
@EJB
ProductLocal productFacadeBean;
@EJB
CartFacade cartFunctions;
private List<ProductEntity> productsList = new ArrayList<>();
private List<StockEntity> stocksList = new ArrayList<>();
private ProductEntity product;
@PostConstruct
private void init(){
setProductsList();
product = new ProductEntity();
}
public void addItemToCart(ProductEntity product) {
int quantity=this.quantityPurchased;
cartFunctions.addItemToCart(product, quantity);
System.out.println(product.toString());
}
// getters+setters
}
The problem is with the <h:selectOneMenu>
to select the quantity. No matter what value is selected, the managed bean always receives a value of 1 for quantity, EXCEPT when the quantity is changed in the last item of the product catalog, in which case the quantity for ALL the items change to the value selected for the last item in the catalog, and the correct quantity is sent to the managed bean.
How is this caused and how can I solve it?