marți, 19 ianuarie 2016

Use a JSF built-in converter to preserve data type

Let's suppose that we have a <h:selectManyListbox> used in the classical way:

<h:form>         
 <h:selectManyListbox value="#{playerBean.selectedRanks}">              
  <f:selectItems value="#{playerBean.playersRanks}"/>
 </h:selectManyListbox>
 <h:commandButton value="Select" action="#{playerBean.selectedAction()}"/>
</h:form>
Basically, the playersRanks is a list of integers that "populates" our list, and the selectedRanks represents the user selections:

@Named
@SessionScoped
public class PlayerBean implements Serializable {

 private ArrayList<Integer> selectedRanks;
 private static final ArrayList<Integer> playersRanks;

 static {
  playersRanks = new ArrayList<>();
  playersRanks.add(1);
  playersRanks.add(2);
  playersRanks.add(3);
 }

 public ArrayList<Integer> getPlayersRanks() {
  return playersRanks;
 }

 public ArrayList<Integer> getSelectedRanks() {
  return selectedRanks;
 }

 public void setSelectedRanks(ArrayList<Integer> selectedRanks) {
  this.selectedRanks = selectedRanks;
 }

 public String selectedAction() {
  // TODO
 }
}

So, the user may select the ranks and submit them without issues/errors. Even if no error occurred, we can notice a "strange" behavior if we try to run the following snippet of code:

<ui:repeat value="#{playerBean.selectedRanks}" var="i">
 #{i}: #{i.getClass()}
</ui:repeat>

The output reveals that the selected ranks are strings, not integers as we expected to see:

1: class java.lang.String
3: class java.lang.String

The explanation relies on the fact that "the generic type information of List<Integer> is lost during runtime and therefore JSF/EL who sees only List is not able to identify that the generic type is Integer and assumes it to be default String (as that's the default type of the underlying HttpServletRequest#getParameter() call during apply request values phase) " - Bauke Scholtz (aka BalusC).

There are two approaches:

·               explicitly specify a Converter
·               use Integer[] instead

In this case, we can use the built-in javax.faces.Integer built-in converter:

<h:form>         
 <h:selectManyListbox value="#{playerBean.selectedRanks}"  
                      converter="javax.faces.Integer">             
  <f:selectItems value="#{playerBean.playersRanks}"/>
 </h:selectManyListbox>
 <h:commandButton value="Select" action="#{playerBean.selectedAction()}"/>
</h:form>

Now, we can perform the same test and the output will be:

1: class java.lang.Integer
3: class java.lang.Integer

Read further explanations here.

Niciun comentariu:

Trimiteți un comentariu