我遇到了SelectableDataModel和ViewScope的问题。以下是我实施的内容: 的facelet:
<p:dataTable id="countries"
value="#{userPreferencesBean.countryDataModel}"
var="country" emptyMessage="No Country found."
selection="#{userPreferencesBean.selectedCountries}"
styleClass="table-no-select-all" dynamic="true" rowKey="#{country.id}">
......
</p:dataTable>
支持Bean:
@ManagedBean(name = UserPreferencesBean.MANAGEDBEAN_NAME)
@ViewScoped
public class UserPreferencesBean implements Serializable {
//All Variable Declarations goes here
@PostConstruct
public void initialize() {
List<Country> coountries = Getting countries from DB
setCountryDataModel(new CountryDataModel(coountries));
}
public void preRender(ComponentSystemEvent event) {
//Nothing here
}
...
}
CountryDataModel:
@ManagedBean(name = CountryDataModel.MANAGEDBEAN_NAME)
@ViewScoped
public class CountryDataModel extends ListDataModel<Country> implements SelectableDataModel<Country>,
Serializable {
public CountryDataModel() {
}
public CountryDataModel(List<Country> countries) {
super(countries);
}
@Override
public Interface getRowData(String rowKey) {
@SuppressWarnings("unchecked")
List<Country> countries = (List<Country>)getWrappedData();
for (Country singleCountry : countries) {
if (isEqualWithRowKey(singleCountry.getId(), rowKey))
return singleCountry;
}
return null;
}
private boolean isEqualWithRowKey(Short countryId, String rowKey) {
Short rowKeyId = Short.parseShort(rowKey);
if (countryId.equals(rowKeyId))
return true;
return false;
}
@Override
public Object getRowKey(Country singleCountry) {
return singleCountry.getId();
}
}
现在发生的事情是第一次正确加载数据表。但是当我点击某一行时,我正在接受NPE。记录如下:
Caused by: java.lang.NullPointerException
at org.primefaces.component.datatable.DataTable.getRowData(DataTable.java:936)
at org.primefaces.component.datatable.feature.SelectionFeature.decodeMultipleSelection(SelectionFeature.java:71)
at org.primefaces.component.datatable.feature.SelectionFeature.decode(SelectionFeature.java:40)
at org.primefaces.component.datatable.DataTableRenderer.decode(DataTableRenderer.java:57)
at javax.faces.component.UIComponentBase.decode(UIComponentBase.java:415)