我想从 Primefaces 的 selectManyCheckbox 菜单中选择值,并将所选值设置为一个属性,以便稍后在写入数据库的方法中使用。这是该组件的 .xhtml 页面中的代码:
<p:selectManyCheckbox id="chkbox1"
value="#{requestBean.filterTypeBean.selectedBooleanFilterTypes}"
layout="pageDirection" converter="filterTypeConverter">
<f:selectItems var="checkbox"
value="#{requestBean.filterTypeBean.listBooleanFilterTypes()}"
itemLabel="#{checkbox.filterTypeName}" itemValue="#{checkbox}" />
</p:selectManyCheckbox>
RequestBean 类是@ViewScoped,在里面我有:
private FilterTypeBean filterTypeBean = new FilterTypeBean(); // Plus public get and set method for it
FilterType 类是@SessionScoped,其中我有:
private List<TFilterType> selectedBooleanFilterTypes; //plus public get and set methods
public List<TFilterType> listBooleanFilterTypes() {
EntityManager em = HibernateUtil.getEntityManager();
Query q = em
.createQuery("select u from TFilterType u where u.filterType = 'B'");
List<TFilterType> resultList = q.getResultList();
return resultList;
}
对我来说,一切似乎都很好,但是当我按下命令按钮时,即使我从 selectManyCheckbox 中选择了一些值,selectedBooleanFilterTypes 列表也是空的。我尝试使用 getSelectedBooleanFilterTypes() 方法获取 RequestBean 类中的值:
List<TFilterType> selectedBooleanFilterTypes = filterTypeBean
.getSelectedBooleanFilterTypes();
似乎未执行 setSelectedFilterTypesNames() 。任何建议这里有什么问题以及如何解决它?提前致谢!
问: 转换器的部分:
public Object getAsObject(FacesContext context, UIComponent component, String value) {
// It will create bean when not done yet.
FilterTypeBean filterTypeBean = context.getApplication().evaluateExpressionGet(context, "#{filterTypeBean}", FilterTypeBean.class);
for (TFilterType type : filterTypeBean.listBooleanFilterTypes()) {
if (type.getFilterTypeName().equals(value)) {
return type;
}
}
return null;
}