RichFaces 4中的java ExtendedDataTable:数据模型处理
我还有一个问题,与我在一月份发布的问题有点相关。我有一个列表,它是丰富的:extendedDataTable组件,当用户在一个单独的文本框中键入搜索条件时(即用户在前4个字符中键入,并且在他不断键入时,结果列表会更改),它会在运行中得到更新。最后,当我使用RichFaces 3时,它工作得很好,但是当我升级到RichFaces 4时,我遇到了各种各样的编译问题。以下类别不再可访问,似乎也没有合适的替代品:
org.richfaces.model.DataProvider
org.richfaces.model.ExtendedTableDataModel
org.richfaces.model.selection.Selection
org.richfaces.model.selection.SimpleSelection
这是以前的情况:
这是应触发搜索逻辑的输入文本:
<h:inputText id="firmname" value="#{ExtendedTableBean.searchValue}">
<a4j:support ajaxSingle="true" eventsQueue="firmListUpdate"
reRender="resultsTable"
actionListener="#{ExtendedTableBean.searchForResults}" event="onkeyup" />
</h:inputText>
操作侦听器是应该更新列表的对象。这是extendedDataTable,位于inputText的正下方:
<rich:extendedDataTable tableState="#{ExtendedTableBean.tableState}" var="item"
id="resultsTable" value="#{ExtendedTableBean.dataModel}">
... <%-- I'm listing columns here --%>
</rich:extendedDataTable>
下面是后端代码,我在其中使用数据模型处理:
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package com.beans;
import java.io.FileInputStream;
import java.util.ArrayList;
import java.util.Collections;
import java.util.ConcurrentModificationException;
import java.util.List;
import java.util.Properties;
import java.util.concurrent.CopyOnWriteArrayList;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;
import org.richfaces.model.DataProvider;
import org.richfaces.model.ExtendedTableDataModel;
public class ExtendedTableBean {
private String sortMode="single";
private ExtendedTableDataModel<ResultObject> dataModel;
//ResultObject is a simple pojo and getResultsPerValue is a method that
//read the data from the properties file, assigns it to this pojo, and
//adds a pojo to the list
private Object tableState;
private List<ResultObject> results = new CopyOnWriteArrayList<ResultObject>();
private List<ResultObject> selectedResults =
new CopyOnWriteArrayList<ResultObject>();
private String searchValue;
/**
* This is the action listener that the user triggers, by typing the search value
*/
public void searchForResults(ActionEvent e) {
synchronized(results) {
results.clear();
}
//I don't think it's necessary to clear results list all the time, but here
//I also make sure that we start searching if the value is at least 4
//characters long
if (this.searchValue.length() > 3) {
results.clear();
updateTableList();
} else {
results.clear();
}
dataModel = null; // to force the dataModel to be updated.
}
public List<ResultObject> getResultsPerValue(String searchValue) {
List<ResultObject> resultsList = new CopyOnWriteArrayList<ResultObject>();
//Logic for reading data from the properties file, populating ResultObject
//and adding it to the list
return resultsList;
}
/**
* This method updates a firm list, based on a search value
*/
public void updateTableList() {
try {
List<ResultObject> searchedResults = getResultsPerValue(searchValue);
//Once the results have been retrieved from the properties, empty
//current firm list and replace it with what was found.
synchronized(firms) {
firms.clear();
firms.addAll(searchedFirms);
}
} catch(Throwable xcpt) {
//Exception handling
}
}
/**
* This is a recursive method, that's used to constantly keep updating the
* table list.
*/
public synchronized ExtendedTableDataModel<ResultObject> getDataModel() {
try {
if (dataModel == null) {
dataModel = new ExtendedTableDataModel<ResultObject>(
new DataProvider<ResultObject>() {
public ResultObject getItemByKey(Object key) {
try {
for(ResultObject c : results) {
if (key.equals(getKey(c))){
return c;
}
}
} catch (Exception ex) {
//Exception handling
}
return null;
}
public List<ResultObject> getItemsByRange(
int firstRow, int endRow) {
return Collections.unmodifiableList(results.subList(firstRow, endRow));
}
public Object getKey(ResultObject item) {
return item.getResultName();
}
public int getRowCount() {
return results.size();
}
});
}
} catch (Exception ex) {
//Exception handling
}
return dataModel;
}
//Getters and setters
}
既然ExtendedTableDataModel和DataProvider类不再可用,我应该用什么来代替呢?RichFaces论坛声称实际上什么都没有,开发人员在这方面几乎是靠自己(这意味着他们必须自己实现)。有人有其他想法或建议吗
再次感谢您的帮助,再次抱歉,我的问题太长了
# 1 楼答案
您可以将数据模型转换为扩展抽象的
org.ajax4jsf.model.ExtendedDataModel
,它实际上是用于<rich:extendedDataTable/>
的更健壮、更高性能的数据模型。将现有模型粗略转换为下面的新模型(我决定使用现有的ExtendedDataModel<ResultObject>
作为基础数据源,而不是results
列表来演示转换):这是该课程中最重要的三种方法。还有很多其他方法,基本上是从遗留版本继承下来的,你不需要担心。如果您正在将JSF状态保存到客户端,那么出于序列化目的,您可能会对
org.ajax4jsf.model.SerializableDataModel
感兴趣。请参见here的示例。这是一个老博客,但逻辑仍然适用与此无关,您当前的
getRowData
实现将在生产级应用程序中表现不佳。必须遍历每个元素才能返回结果?试试更好的搜索算法