val*_*ian 5 error-handling lazy-loading growl primefaces
我无法让用户知道PrimeFaces LazyDataModel#load
方法中发生的异常。
我正在从数据库加载数据,当引发异常时,我不知道如何通知用户。
我尝试添加FacesMessage
到FacesContext
,但即使 Growl 设置为 ,消息也不会显示在 Growl 组件上autoUpdate="true"
。
它不起作用,因为当所有消息都已被处理时,load()
方法是在渲染响应阶段调用的(您可以通过打印来检查)。FacesContext.getCurrentInstance().getCurrentPhaseId()
对我有用的唯一解决方法是在数据表的“页面”事件侦听器中加载数据。
html:
<p:dataTable value="#{controller.model}" binding="#{controller.table}">
<p:ajax event="page" listener="#{controller.onPagination}" />
</p:dataTable>
Run Code Online (Sandbox Code Playgroud)
控制器:
private List<DTO> listDTO;
private int rowCount;
private DataTable table;
private LazyDataModel<DTO> model = new LazyDataModel<DTO>() {
@Override
public List<DTO> load(int first, int pageSize, String sortField,
SortOrder sortOrder, Map<String, String> filters) {
setRowCount(rowCount);
return listDTO;
}
};
public void onPagination(PageEvent event) {
FacesContext ctx = FacesContext.getCurrentInstance();
Map<String, String> params = ctx.getExternalContext()
.getRequestParameterMap();
// You cannot use DataTable.getRows() and DataTable.getFirst() here,
// it seems that these fields are set during Render Response phase
// and not during Update Model phase as one can expect.
String clientId = table.getClientId();
int first = Integer.parseInt(params.get(clientId + "_first"));
int pageSize = Integer.parseInt(params.get(clientId + "_rows"));
try {
listDTO = DAO.query(first, pageSize);
rowCount = DAO.getRowCount();
} catch (SQLException e) {
ctx.addMessage(null,
new FacesMessage(FacesMessage.SEVERITY_ERROR,
"SQL error",
"SQL error"));
}
}
Run Code Online (Sandbox Code Playgroud)
希望这可以帮助。
归档时间: |
|
查看次数: |
3765 次 |
最近记录: |