0

我想从 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;
    }
4

1 回答 1

0

这可以帮助我解决我的问题:

@WebFilter("*.jsf")
public class CharacterEncodingFilter implements Filter {

    @Override
    public void doFilter(ServletRequest request, ServletResponse response,
            FilterChain chain) throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");
        chain.doFilter(request, response);
    }

    @Override
    public void destroy() {
        // TODO Auto-generated method stub  
    }

    @Override
    public void init(FilterConfig arg0) throws ServletException {
        // TODO Auto-generated method stub

    }
}

我按照这个在 JSF 中提交的 UTF-8 表单正在破坏数据链接。

于 2014-05-01T15:02:33.797 回答