gpt4 book ai didi

jsf - CommandLink 不适用于延迟加载的 Primefaces Datascroller

转载 作者:行者123 更新时间:2023-12-04 21:41:25 26 4
gpt4 key购买 nike

我在延迟加载 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 上运行

最佳答案

我在此链接 http://www.theserverside.com/news/thread.tss?thread_id=44186 中找到了有关请求范围行为的很好解释

If you are using ManagedBeans in request scope, you get problems with CommandLinks inside DataTables. DataTables are one thing I really like about JSF, and CommandLinks often come in handy as well. But when you put a CommandLink inside a DataTable, e. g., to select the entry of the row in which the CommandLink is, you get bitten. That is, if you want ManagedBeans with request scope. The action which should be triggered by the CommandLink is never triggered, the page is simply rendered again. The reason for this behaviour is that the DataTable modifies the id of the CommandLink during renderering, but the CommandLink does not know that it was rendered with a different id. During the decoding of the request which was triggered by clicking the CommandLink, the ComandLinkRenderer looks at a hidden form parameter. If the value of that form parameter equals the id of the CommandLink, an action is queued. If not, nothing is done. Since the DataTable changes the ids, the value of the hidden form parameter does not match the id of the CommandLink.



基于上述上下文,您需要将范围注释从 更改为@ViewScoped
@SessionScope ,您的问题将自动解决。这似乎是比编写额外代码更好的解决方案,除非您需要保留 @ViewScopped

关于jsf - CommandLink 不适用于延迟加载的 Primefaces Datascroller,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25409316/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com