0

I have some problems with my JSF page which displays a list of pizzas. I go through each entry of my ArrayList which holds the pizza data with the ui:repeat loop. For each pizza the user can choose the size and the quantity before the user can add the pizza into the cart by clicking on a button. This is where the error occurs. By clicking the button, the JSF page refreshes and it happens nothing. There is no output on my console..

JSF-page code snippet:

<ui:repeat var="result" value="#{pizzaSearchBean.resultList}">
<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="pizzasize" value="#{pizzaSearchBean.pizzasize}">
            <f:selectItems value="#{result.sizeList} var="size" itemLabel="#{size.diameter}" itemValue="#{size.sizeID}"/>
         </h:selectOneRadio>
      </li>
      <li>
         <p>Quantity:</p>
         <h:selectOneListbox id="pizzaquantity" value="#{pizzaSearchBean.pizzaquantity}" size="1">
            <f:selectItem id="quantity1" itemLabel="1x" itemValue="1">
            <f:selectItem id="quantity2" itemLabel="2x" itemValue="2">
         </h:selectOneListbox>
      </li>
      <li>
         <!-- this is where no action happens... -->
         <h:commandButton value="add to cart" action="#{pizzaSearchBean.addToCart(result.pizza.pizzaID)}"/>
      </li>
   </ul>
</h:form>
</ui:repeat>

In the managedBean class there are several classes to manage all the data. The addToCard method looks just as simple as this:

public void addToCart(int pizzaid) {
   System.out.println("pizza ID : " + pizzaid);
}

Everything works instead of the commandButton action method "addToCart()". No matter what is written inside the action tag of commandButton. After rendering the pizza search and clicking onto one of the pizza's "add to cart"-button there still will be no action..

I hope you can help me.. Thanks!

user3548416
  • 117
  • 1
  • 1
  • 11
  • Are there any errors at all when you debug? – Josef E. Aug 05 '14 at 14:30
  • Can you post the code for the `pizza` bean and the `pizzaSearchBean` too? – BackSlash Aug 05 '14 at 14:31
  • I think your problem is that you duplicate the `h:form` tag. Is that what you want? A form to each of the iteration? – Jorge Campos Aug 05 '14 at 14:35
  • There are no errors after debugging. Exactly, I want a form for each pizza object, which I'm iterating with the ui:repeat tag. So that for each pizza object there is a button for the user to add this pizza to the cart. – user3548416 Aug 05 '14 at 14:39
  • My bean and the other classes seem to be correct. There are neither runtime errors, nor other problems. – user3548416 Aug 05 '14 at 14:50
  • http://stackoverflow.com/questions/2118656/hcommandlink-hcommandbutton-is-not-being-invoked Did you exclude among others #4? – BalusC Aug 05 '14 at 15:21
  • I did but the commandButton still seems to be dead. I can enter what I want like a redirection back to the home screen (action="index.xhtml"), all the page does is to refresh. – user3548416 Aug 05 '14 at 15:35
  • Which JSF impl/version? Tried the latest impl version? – BalusC Aug 05 '14 at 16:42
  • Yesterday I went through #4 again of your link @BalusC. Seems like this is the source of the problem. So I've changed my managedBean to ViewScoped and I've tried to provide the value of the component (resultList) in a post construct. But right now I'm standing here with the question of how to return **exactly** the same value of the component, **when the search results are only initialized while the JSF page is already opened**? So how can I achieve that the managedBean returns the exactly same value of the component when the component is initialized later on (as #4 of your link described)? – user3548416 Aug 06 '14 at 08:07
  • Just keep getters true getters. – BalusC Aug 06 '14 at 08:09
  • But how can I provide a list already by opening the managedBean if the list is only initialized later during the session? @BalusC – user3548416 Aug 06 '14 at 08:14
  • Stop. Have you tried it? Putting bean in view scope, keeping getters true getters and filling list in action method. Are you facing problems? If so, which exactly? It doesn't sound like you actually tried it, because the question makes no sense. Why would you need to fill a list without user's input if the list is actually dependent on user's input? Just put the bean in view scope and keep getters true getters. That's all! It doesn't matter when you fill the list, as long as it's not done in a getter. – BalusC Aug 06 '14 at 08:17
  • Yes I have tried it and unfortunately still facing the same problem. My bean is now viewScoped, there are no actions in getter/setter but the get and set functions and I am filling the list by the action tag of the commandButton which calls the initialize method and fills the list. The JSF page still keeps refreshing and doesn't display any pizza objects. – user3548416 Aug 06 '14 at 08:29
  • @BalusC first of all thank you for your help! But for misunderstandings here the process of the program: user opens JSF page which now only shows a search form for pizzas on the left side (list is empty). After filling and submitting the form, the same JSF page displays the pizza objects generated by the search parameters (list is now filled). Each pizza object has it's own "add-to-cart-button". By clicking one of these buttons nothing happens but the JSF page refreshes and the pizza objects aren't there anymore. All action happen in one managedBean. – user3548416 Aug 06 '14 at 08:37
  • In other words, either you wasn't actually using the view scope, or it was destroyed and recreated. – BalusC Aug 07 '14 at 05:29

0 Answers0