categories; private Lis" /> categories; private Lis" /> categories; private Lis"/>

h:selectManyCheckbox converter's getAsObject always retrieves "on" as submitted value

546 Views Asked by At

I have a bean:

@ManagedBean(name = "bExam")
@SessionScoped
public class BExam implements Serializable
{
    private List<Category> categories;
    private List<Category> categoriesSelected;

    public BExam() {

        categories = CategoryDb.getAll(); // there is ok. Categories has filled right.
        categoriesSelected = new ArrayList<>();

        getters & setters...
 }

There is converter:

@FacesConverter("categoryConverter")
public class CategoryConverter implements Converter<Category> {

     @Override
     public Category getAsObject(FacesContext fc, UIComponent uic, String string) {
          ...
     }

     @Override
     public String getAsString(FacesContext fc, UIComponent uic, Category t) {
          return String.valueOf(t.getId());
     }
}

There is selectManyCheckbox:

<h:selectManyCheckbox id="categories" value="#{bExam.categoriesSelected}" converter="categoryConverter">
     <f:selectItems
           value="#{bExam.categories}"
           var="category"
           itemLabel="#{category.name}"
           itemValue="#{category}"/>
</h:selectManyCheckbox> 

And this is Category model:

public class Category implements Serializable
{
     private int id;
     private String name;
     private int sortOrder;
     private int categorySetId;

     getters & setters...
}

Checkboxes are builds right. When i click on any of them, in getAsObject in string parameter i always get "on". enter image description here

What's happening there? Why this string comes exactly? And how solve it?

1

There are 1 best solutions below

2
BalusC On

When I click on any of them, in getAsObject in string parameter I always get "on"

This is the browser default value for a checked checkbox without value. This will be used when HTML <input type="checkbox"> doesn't have a value attribute or it is empty.

By default, JSF renders getAsString() value there. So, it can happen when the actual converter returns null or an empty string in getAsString(). It can also happen when the (default) HTML Renderer associated with the <h:selectManyCheckbox> is being overridden in an incorrect manner, or is even corrupted (e.g. the default HTML Renderer associated with <h:selectBooleanCheckbox> is in some way being used instead).

The cause of the problem is not visible in the information provided so far, but your first step is putting a debug breakpoint in getAsString() of the converter and explore the call stack which renderer called it, and inspect which value the converter returns.