JSF commandButton动作被多次调用

时间:2014-08-08 14:12:17

标签: jsf foreach javabeans

我的JSF页面有问题。

该计划:用户可以通过输入表单来搜索比萨饼。过滤搜索是可能的,因为用户可以决定他是否搜索比萨饼名称,比萨饼ID或其他指定标准。该程序将生成一个SQL查询,该查询返回pizza对象并将其存储到对象列表中。 JSF页面通过ui:repeat标记迭代它们来显示披萨对象列表。将显示比萨饼名称,比萨饼ID,可用尺寸(显示为单选按钮)和可能数量列表。对于每个显示的披萨对象,都有一个"添加到购物车按钮",用于在所选尺寸和数量的参数化值下将披萨添加到购物车。

问题:几乎所有内容都正确显示。但是当在购物车上添加比萨饼时,会发生错误。如果用户决定披萨,请选择它的大小和数量,然后点击比萨饼的命令按钮"添加到购物车"。调用方法addToCart()并简单地返回提交的参数(pizzaID,selectedSize,chosenQuantity)。但不知何故,该方法将被多次调用。

JSF页面:

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

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

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
}

正如我在之前的问题(JSF & ui:repeat - issue with adding an object to cart)中写的那样,我已经使用了ui:repeat标签,但没有发生多次调用的问题。但我必须找到另一个解决方案来迭代我的披萨对象列表,这就是我使用c:forEach的原因。我也不想从Majorra换到MyFaces。

我希望你能以某种方式帮助我。谢谢!

0 个答案:

没有答案
相关问题