سؤال

I want to select values from Primefaces selectManyCheckbox menu and set the selected values to a property which to use later in a method for writing into database. Here is the code from .xhtml page for the component:

<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 class is @ViewScoped and inside it i have:

private FilterTypeBean filterTypeBean = new FilterTypeBean(); // Plus public get and set method for it

FilterType class is @SessionScoped and in it i have :

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;
        }

For me, everything seems ok, but when i press the commandButton, the selectedBooleanFilterTypes list is empty even if i select some values from the selectManyCheckbox. I try to take the values in RequestBean class using getSelectedBooleanFilterTypes() method:

List<TFilterType> selectedBooleanFilterTypes = filterTypeBean
                .getSelectedBooleanFilterTypes();

It seems that setSelectedFilterTypesNames() is not executed. Any suggestion what is the problem here and how to fix it? Thanks in advance!

Asked: The part from the Converter:

  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;
    }
هل كانت مفيدة؟

المحلول

This help me to fix my problem:

@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

    }
}

I follow this UTF-8 form submit in JSF is corrupting data link.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top