在<p:datalist> </p:datalist> </p:selectbooleanbutton>中没有为<p:selectbooleanbutton>触发AJAX监听器

时间:2013-02-24 15:41:07

标签: jsf-2 primefaces

我正在尝试使用<p:dataList>为页面上显示的alist f项目实现“添加项目到购物车”功能。添加按钮是p:selectBooleanButton

我在按钮上附加了一个ajax监听器,将当前项添加到我的支持bean中的一个集合中。但是,不调用侦听器方法,而是按钮呈现更新。

附加valueChangeListener作品,但我认为我不应该使用它?

然而,在任何一种情况下,我的分页都会停止工作。单击任何分页按钮不会更改页面内容。

有人可以请求相同或建议更好的方法吗?

以下是代码:

支持Bean

@ManagedBean(name = "movies")
@ViewScoped
public class MovieListBean extends BaseBean implements Serializable
{
   private static final long serialVersionUID = -1887386711644123475L;

   private LazyDataModel<Movie> lazyModel;

   private Map<Integer, Rental> cart;

   @PostConstruct
   public void initialize() {
      cart = new HashMap<>(0);
   }

   public LazyDataModel<Movie> getLazyModel() 
   {
      if (lazyModel == null) 
      {
         lazyModel = new LazyDataModel<Movie>() {

            private static final long serialVersionUID = -858683609299266223L;

            @Override
            public List<Movie> load(int first, int pageSize, String sortField, SortOrder sortOrder, Map<String, String> filters) 
            {
               List<Movie> movieList = serviceLocator.getMovieService().getMovieCatalogInRange(first, (first + pageSize));
               this.setRowCount(serviceLocator.getMovieService().getCatalogSize());
               return movieList;
            }
         };
      }
      return lazyModel;
   }

   public void updateCart(Object obj)
   {
      System.out.println("Selected Movie ID" + obj);
      if (lazyModel != null)
      {
         System.out.println("Selected row in datalist - " + lazyModel.getRowIndex());
         System.out.println("Object instance associated with selected row - " + lazyModel.getRowData());
      }
      // Process object instance and add to cart
   }

   public Map<Integer, Rental> getCart() {
      return cart;
   }

   public void setCart(Map<Integer, Rental> cart) {
      this.cart = cart;
   }

}

的index.xhtml

<ui:composition template="/WEB-INF/templates/template.xhtml">
   <ui:define name="content">
      <h:outputStylesheet name="movies.css" library="styles" />

      <h:form prependId="false" id="form">
         <p:dataList value="#{movies.lazyModel}" var="movie" id="movies" paginator="true" rows="10"
            paginatorTemplate="{FirstPageLink} {PreviousPageLink} {PageLinks} {NextPageLink} {LastPageLink}"
            type="none" paginatorAlwaysVisible="false" lazy="true">

            <p:panel id="movieInfo">
               <h:outputText value="#{movie.movieName}" styleClass="titles" />
               <div id="infoContainer">
                  <div>
                     <h:graphicImage library="images" name="#{movie.imageUri}" />
                  </div>
                  <div>
                     <div>
                        <h:outputText value="#{movie.plotLine}" />
                     </div>
                     <div class="rating">
                        <span>Rating : </span>
                        <p:rating value="#{movie.rating}" stars="10" readonly="true" />
                     </div>
                     <div id="rent">
                        <p:selectBooleanButton offLabel="Add to Cart" onLabel="Remove from Cart" id="btnRent"
                        value="#{not empty movies.cart[movie.movieID]}">
                           <p:ajax update="btnRent" listener="#{movies.updateCart (movie.movieID)}" event="click"/>
                        </p:selectBooleanButton>
                     </div>
                  </div>
               </div>
            </p:panel>

         </p:dataList>
      </h:form>

   </ui:define>
</ui:composition>

我正在使用JSF 2.1(Mojarra)+ Primefaces 3.5

1 个答案:

答案 0 :(得分:1)

p:selectBooleanButton的值为#{not empty movies.cart[movie.movieID]}。这是不可能的。它应该是一个布尔左值,一些带有getter和setter的属性。您认为JSF在使用AJAX发送时会设置字段吗?由于此按钮位于数据表中,您可以将它们的值组织为布尔数组。