我在延迟加载Primefaces Datascroller组件时遇到问题。
我有一个 jsf 页面,应该在页面加载时显示 10 个事件。如果用户想看更多,他/她可以点击更多按钮来加载并显示接下来的 10 个事件。对于每个事件行,都有一个链接可用于显示事件的详细信息。
<h:form id="mainForm" >
<p:dataScroller value="#{backing.lazyModel}" var="event" lazy="true" chunkSize="10" rowIndexVar="index">
#{event.name}
<p:commandLink class="view-trigger"
value="View Event Details"
actionListener="#{backing.initViewEventDetails(index, event)}"/>
<f:facet name="loader">
<p:outputPanel
visible="#{backing.lazyModel.rowCount gt 10}"
rendered="#{backing.lazyModel.rowCount gt 10}">
<p:commandLink value="More" />
</p:outputPanel>
</f:facet>
</p:dataScroller>
</h:form>
初始搜索工作正常,也就是说,当我单击查看事件详细信息链接时,我的支持 bean 被调用,我看到收到的索引和事件对应于我单击的行。
但是,一旦我加载包含 1 个额外事件的下一个块,页面会显示 11 个事件,但单击查看事件详细信息链接会发送正确的索引,但不会发送正确的事件。例如,如果我单击索引 0 处的事件,我将获得索引 10 处的事件,如果我单击索引 1 处的事件,则不会调用我的支持 bean。
当我单击更多按钮时,数据滚动器似乎忘记了最后 10 个事件,但我的惰性数据模型仍然记得。
支持bean:
@ManagedBean(name="backing")
@ViewScoped
public class DataScrollerBacking implements Serializable {
private static final long serialVersionUID = 4012320411042043677L;
private static final Logger LOGGER = Logger.getLogger(DataScrollerBacking.class);
@ManagedProperty("#{settings.dataSource}")
private String dataSource;
private WebEventDAO webEventDAO;
private LazyDataModel<Event> lazyModel;
@PostConstruct
public void init() {
webEventDAO = CommonDAOFactory.getInstance(dataSource).getWebEventDAO();
search();
}
public void search() {
DateTime start = new DateTime(2014, 1, 1, 0, 0 ,0);
final Date startDate = start.toDate();
final Date endDate = start.plus(Years.ONE.toPeriod()).minus(Seconds.ONE.toPeriod()).toDate();
lazyModel = new LazyDataModel<Event>() {
private static final long serialVersionUID = 1231902031619933635L;
private LinkedHashSet<Event> eventCache; // Ordered set of all retrieved events so far.
// I'm using a set because the load method is called twice on page load (any idea why???) and I don't want duplicates in my cache.
@Override
public List<Event> load(int first, int pageSize, String sortField, SortOrder sortOrder, Map<String, Object> filters) {
List<Event> events = new ArrayList<Event>(10);
try {
if(eventCache == null){
int count = webEventDAO.getSearchByPeriodRaceTypeAndRaceStatusForCompanyCount(Collections.singletonList(1), startDate, endDate, null, null);
this.setRowCount(count);
eventCache = new LinkedHashSet<Event>(count);
}
events = webEventDAO.searchByPeriodRaceTypeAndRaceStatusForCompany(Collections.singletonList(1), startDate, endDate, null, null, true, first, pageSize);
eventCache.addAll(events);
} catch (DAOException e) {
LOGGER.error("An error occurred while retrieving events.", e);
}
return events;
}
};
}
public void initViewEventDetails(Integer index, Event event){
LOGGER.info("index=" + index + " eventname=" + event.getName());
}
public String getDataSource() {
return dataSource;
}
public void setDataSource(String dataSource) {
this.dataSource = dataSource;
}
public LazyDataModel<Event> getLazyModel() {
return lazyModel;
}
public void setLazyModel(LazyDataModel<Event> lazyModel) {
this.lazyModel = lazyModel;
}}
由于页面显示正确的信息并且接收到的索引始终有效,因此我当前的解决方法是通过索引在惰性数据模型中获取事件。
但是,我想了解为什么收到的事件不是我点击的那个。
我做错了什么还是这就是滚动条的实现方式?
在 Mojarra 2.2、Tomcat 7、Primefaces 5、Omnifaces 1.8 上运行