0

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;
    }
Tot
  • 207
  • 8
  • 25
  • First of all move all that logic out of the `listBooleanFilterTypes`. [Don't do business logic in a getter](http://stackoverflow.com/a/14570012/1530938). Then examine whether or not conversion isn't failing and the setter is being called – kolossus Apr 30 '14 at 19:37
  • On the first load the SET method is still not executed, it gives an error form:: Validation Error: Value is not valid. When i reload the page, the sett-er set the method and it works, but i can not understand why it makes problem every 1st load after restarting the server and run the application. – Tot May 01 '14 at 07:31
  • This error comes from the converter because the value in the property is null. How to force the value to be set-ed on the first load of the page? If i did not explain something well, please ask me! – Tot May 01 '14 at 11:41
  • Have you moved the logic out of `listBooleanFilterTypes`? – kolossus May 01 '14 at 12:43
  • Yes, but it seems the problem is other. I just debugged it and i saw that there is a value in the property, but on the first commit encoding is not the same and it is not able to convert it from String to Object (in getAsObject method) and returns NULL. Any ideas? – Tot May 01 '14 at 12:59
  • Good. Now that you've gotten that out of the way, [look here](http://stackoverflow.com/questions/9069379/validation-error-value-is-not-valid/9069660#9069660). Then post your converter here – kolossus May 01 '14 at 13:06
  • The Converter method is added in the question. – Tot May 01 '14 at 13:27
  • Have you gone through the possible causes listed in the answer that I referenced? – kolossus May 01 '14 at 14:47
  • Thanks to this post: http://stackoverflow.com/questions/10721342/utf-8-form-submit-in-jsf-is-corrupting-data, it works now. Thank you too for the help! – Tot May 01 '14 at 14:59

1 Answers1

0

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.

Community
  • 1
  • 1
Tot
  • 207
  • 8
  • 25