Getter / Setter for List
i have one method followed:
public List<Car> getCart(){
//do something
}
and in JSF Page i user this method for display Data in h:dataTable
and in view (JSF Page) i have one inputText in h:dataTable, example in h:dataTable i have 2 column are Cart Name and Quantity, and column Quantity is inputText for user edit Quantity of item. My Problem is how can i set value for List dataType? because i have formular for update cart follow:
Quantity of Cart Update = (Item In stock + old Quantity of each item) - new Quantity of each item
in upon formula i can get item instock , but i wonder between old Quantity of each item and new Quantity of each item because if i get Quantity of each item it will new value. But now i just only get value of old Quantity of each item, because i don't know how can setter for List
Everybody can have any ideas for this problem and my formula?
Thank you
===============================================
Edit
my List Cart method in backin bean
public List<Cart> getUserCart(){
int userid = getMemberLoginController().getUser().getUserid().intValue();
return cartDAO.getUSerCart(userid);
}
cartDAO is Session Bean Stateless it do getResultList where userid = :userid.
and update method
public void updateCart(){
List<Cart> cartQuantity = getUserCart();
int i = 0 ;
for(Cart cartQuantityOnHand : cartQu开发者_如何学JAVAantity){
i = cartQuantityOnHand.getCartQuantity();
int a = i + cartQuantityOnHand.getItem().getInstock();
System.out.println(i);
}
}
how can i get old Quantity on hand of List and new Quantity on hand of List?
The list doesn't need a setter. JSF will only call its getter and then call the setter on each of the list's items.
Your problem sounds like as if you're loading the list inside the getter everytime instead of in bean's constructor or postconstruct. If you're getting the list by its getter in the bean's action method, then the original list with all submitted values will be overridden with the new one from the DB.
So, you should NOT design it as:
public List<Item> getItems() {
return em.createNamedQuery("Item.list", Item.class).getResultList();
}
public String submit() {
for (Item item : getItems()) {
em.persist(item);
}
return "outcome";
}
But you should rather design it as:
private List<Item> items;
@PostConstruct
public void init() {
items = em.createNamedQuery("Item.list", Item.class).getResultList();
}
public List<Item> getItems() {
return items;
}
public String submit() {
for (Item item : items) {
em.persist(item);
}
return "outcome";
}
精彩评论