使用JSF PrimeFaces时,在java.lang.String类型上不可读的属性
我正在尝试使用站点中的演示代码在数据表中实现延迟加载数据
PrimeFaces懒人加载
我收到了错误
javax.el.PropertyNotFoundException: /table.xhtml @14,49 value="#{car.year}": Property 'year' not readable on type java.lang.String
这是我的table.xhtml代码
TableBean代码
@ManagedBean public class TableBean { private LazyDataModel lazyModel; private List cars; public TableBean() { System.out.println("Girish"); cars = populateRandomCars(50); lazyModel = new LazyCarDataModel(cars); lazyModel.setRowCount(10); } public LazyDataModel getLazyModel() { return lazyModel; } public void setLazyModel(LazyDataModel lazyModel) { this.lazyModel = lazyModel; } public List getCars() { return cars; } public void setCars(List cars) { this.cars = cars; } private List populateRandomCars( int size) { List list = new ArrayList(); Car car = null; for(int i = 0 ; i < size ; i++) { car = new Car(); car.setColor("color "+i); car.setYear(""+i); list.add(car); } return list; } }
车密码
class Car { private String year; private String color; public String getYear() { return year; } public void setYear(String year) { this.year = year; } public String getColor() { return color; } public void setColor(String color) { this.color = color; } }
和LazyCarDataModel代码
public class LazyCarDataModel extends LazyDataModel { /** * */ private static final long serialVersionUID = 1L; private List datasource; public LazyCarDataModel(List datasource) { this.datasource = datasource; } @Override public Car getRowData(String rowKey) { return new Car(); } @Override public Object getRowKey(Car car) { return car.getYear(); } @Override public List load(int first, int pageSize, String sortField, SortOrder sortOrder, Map filters) { List data = new ArrayList(); //filter for(Car car : datasource) { boolean match = true; for(Iterator it = filters.keySet().iterator(); it.hasNext();) { try { String filterProperty = it.next(); String filterValue = filters.get(filterProperty); String fieldValue = String.valueOf(car.getClass().getField(filterProperty).get(car)); if(filterValue == null || fieldValue.startsWith(filterValue)) { match = true; } else { match = false; break; } } catch(Exception e) { match = false; } } if(match) { data.add(car); } } //rowCount int dataSize = data.size(); this.setRowCount(dataSize); //paginate if(dataSize > pageSize) { try { return data.subList(first, first + pageSize); } catch(IndexOutOfBoundsException e) { return data.subList(first, first + (dataSize % pageSize)); } } else { return data; } } }
请帮忙。
提前致谢。
模型类,在这种情况下, Car
必须是公开的。