我正在尝试使用<p:dataList>
为页面上显示的alist f项目实现“添加项目到购物车”功能。添加按钮是一个p:selectBooleanButton。AJAX监听器没有被解雇为<p:selectBooleanButton>里面<p:dataList>
我已经附加了一个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(钻嘴鱼科)+ Primefaces 3.5
但这不是表达式的最终值是真是假吗?或者它实际上是否必须是布尔属性?也许我错过了一些东西,但唯一的办法就是找出按钮状态是否应该被切换,就是购物车中物品的存在 – Vrushank 2013-02-24 18:24:38
你第一次阅读,是的。但JSF尝试在应用请求阶段设置值,在这里这是不可能的,因为这不是左值(你不能赋值给它,只是读)。当你绑定两个定向绑定的JSF中的'value'属性时,读取和写入。 'selectBooleanButton'是输入组件,因此它不仅读取它也写入的值。 – partlov 2013-02-24 18:27:35
Partlov是绝对正确的。组件的“值”的连接是双向连接,与“仅评估”属性相反,例如渲染。在这些属性中,你可以指定'bean.field ne null'等。 – skuntsel 2013-02-24 19:50:04