My JSF Books/Videos My JSF Tutorials OmniFaces/JSF PPTs
JSF 2.3 Tutorial
JSF Caching Tutorial
JSF Navigation Tutorial
JSF Scopes Tutorial
JSF Page Author Beginner's Guide
OmniFaces 2.3 Tutorial Examples
OmniFaces 2.2 Tutorial Examples
JSF Events Tutorial
OmniFaces Callbacks Usages
JSF State Tutorial
JSF and Design Patterns
JSF 2.3 New Features (2.3-m04)
Introduction to OmniFaces
25+ Reasons to use OmniFaces in JSF
OmniFaces Validators
OmniFaces Converters
JSF Design Patterns
Mastering OmniFaces
Reusable and less-verbose JSF code

My JSF Resources ...

Java EE Guardian
Member of JCG Program
Member MVB DZone
Blog curated on ZEEF
OmniFaces is an utility library for JSF, including PrimeFaces, RichFaces, ICEfaces ...

.

.

.

.

.

.

.

.


[OmniFaces Utilities] - Find the right JSF OmniFaces 2 utilities methods/functions

Search on blog

Petition by Java EE Guardians

Twitter

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

JSF BOOKS COLLECTION

Postări populare

Visitors Starting 4 September 2015

Locations of Site Visitors