0

I have a problem with my JSF page.

The program: A user can search for pizzas by entering a form. A filtered search is possible as the user can decide whether he searches for a pizza name, a pizza id or other specified criteria. The program will generate a SQL query which returns pizza objects and stores it into a list of objects. A JSF page displays the list of pizza objects by iterating them through a ui:repeat tag. Pizza name, pizza ID, available sizes (displayed as radio buttons) and a list of possible quantities are displayed. For each displayed pizza object there is an "add-to-cart-button", to add the pizza to the shopping cart under the parameterized values of the chosen size and quantity.

The problem: Almost everything is displayed correctly. But when it comes to add a pizza to the cart, the error occurs. If the user decides for a pizza, chose it's size and quantity he clicks on the pizza's commandButton "add to cart". The method addToCart() is invoked and simply returns the submitted parameters (pizzaID, chosenSize, chosenQuantity). But somehow the method will be invoked multiple times.

The JSF page:

<c:forEach var="result" items="#{pizzaResults.results}">
<h:form>
   <ul>
      <li><p>Name: #{result.pizza.name}</p></li>
      <li><p>ID: #{result.pizza.pizzaID}</p></li>
      <li>
         <p>Toppings:</p>
         <ui:repeat var="topping" value="#{result.toppingList}">
            <p>#{topping.toppingName}</p>
         </ui:repeat>
      </li>
      <li>
         <p>Sizes:</p>
         <h:selectOneRadio id="chosenSize" value="#{pizzaResult.chosenSize}">
            <f:selectItems value="#{result.sizeList} var="size" itemLabel="#{size.diameter}" itemValue="#{size.sizeID}"/>
         </h:selectOneRadio>
      </li>
      <li>
         <p>Quantity:</p>
         <h:selectOneListbox id="chosenQuantity" value="#{pizzaResult.chosenQuantity}" size="1">
            <f:selectItem id="quantity1" itemLabel="1x" itemValue="1">
            <f:selectItem id="quantity2" itemLabel="2x" itemValue="2">
         </h:selectOneListbox>
      </li>
      <li>
         <h:commandButton value="add to cart" action="#{pizzaResult.addToCart(result.pizza.pizzaID)}"/>
      </li>
   </ul>
</h:form>
</c:forEach>

The bean PizzaSearch:

@ManagedBean
@SessionScoped
public class PizzaSearch {

   // variables in order to submit the search criteria
   private List<PizzaObject> results = new ArrayList<PizzaObject>();

   // methods to generate the search
   // each search result will fill/replace the list of pizza objects 'results'

   // getter and setter methods, just like
   public List<PizzaObject> getResults() {
      return results;
   }
}

The bean PizzaResult:

@ManagedBean
@SessionScoped
public class PizzaResult {

   // injection of PizzaSearch
   @ManagedProperty(value="#{pizzaSearch}")
   private PizzaSearch pizzaSearch;

   // variables
   private List<PizzaObject> results;
   private int _chosenSize;
   private int _chosenQuantity;

   @PostConstruct
   public void initResults() {
      this.results = pizzaSearch.getResults();
   }

   // this method is being invoked multiple times
   // method to add the pizza object to the cart
   public void addToCart(int chosenPizzaID) {
      System.out.println("chosen pizza ID: " + chosenPizzaID);
      System.out.println("chosen size:     " + _chosenSize);
      System.out.println("chosen quantity: " + _chosenQuantity);
   }

   // getter and setter methods
}

As I wrote in my question before (JSF & ui:repeat - issue with adding an object to cart), I have already used the ui:repeat tag with which the problem of multiple invokes have not occurred. But I had to find another solution to iterate through my list of pizza objects and that is why I am using c:forEach. Also I do not want to change from Majorra to MyFaces.

I hope you can help me somehow. Thanks!

Community
  • 1
  • 1
user3548416
  • 117
  • 1
  • 1
  • 11
  • 1
    h:form tag should be outside c:forEach. Try using ui:repeat with f:setPropertyActionListener for setting selected pizza. – kaos Aug 08 '14 at 14:15
  • If I move h:form outside, errors will be thrown. And that's not what I want. I really want one "add-to-cart-button" for each pizza displayed. As I already mentioned the ui:repeat has a bug under Majorra so I can't use ui:repeat. See http://stackoverflow.com/questions/12808878/uiform-within-uirepeat-not-working/12824677#12824677 – user3548416 Aug 08 '14 at 14:26
  • What errors? Something with duplicate id's? – kaos Aug 08 '14 at 14:41
  • exactly. There is an error that tells me that some ID is already used. – user3548416 Aug 08 '14 at 14:45
  • Try adding varStatus="status" to c:forEach. Than in id's append index e.g. id="chosenQuantity_#{status.index}" – kaos Aug 08 '14 at 15:01

0 Answers0