0

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?

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
Df.fpm
  • 189
  • 1
  • 5
  • 15
  • Checkboxes's `value` attribute is empty..And i cant understand why.. Categories have data in each property. – Df.fpm Jul 12 '16 at 14:00

1 Answers1

3

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.

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • I did spend too much time for searching problem, but unforchantly didn't found. Then i decided use `` approach. Thanks BalusC for your answer. – Df.fpm Jul 14 '16 at 06:09
  • As answered, 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. As long as you don't tell or ask anything about that, we can't advance to the next step. – BalusC Jul 14 '16 at 08:51