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, 28 iunie 2016

JSF 2.3 - Import constants/enums

Let's checkout a common practice for declaring constants in Java using the public static final declaration:

public class Circle {
 private static final float PI = 3.14f;
 private static final String UNIT = " radians";
 ...
}

Also, Java interfaces and enums are very useful in applications (they are part of the Java language ʺbricksʺ):

public interface Car {
 public String MODEL = "Logan";
 public String COUNTRY = "RO";
}

public enum Cars {
 CITROEN, LOGAN, BMW;
}

Now, letʹs suppose that we have these artifacts in a JSF application, as a JSF page author we need to use them in page via EL, as below:

·         Use constants:

#{Circle.PI}
#{Circle.UNIT}

·         Use interfaces and enums:

#{Car.MODEL}

There is no elegant way to say that the above usages will simply not work!

So, now let's consider the following example. First, we define an enum:

public enum PlayerEnum {

 FIRST, SECOND, THIRD;

 public Integer getRank() {
  switch (name()) {
          case "FIRST":
                return 1;
          case "SECOND":
                return 2;
          case "THIRD":
                return 3;
          default:
                return 0;
  }
 }
}

Further, we use this enum in a CDI managed bean:

@Named
@RequestScoped
public class PlayerBean implements Serializable {

 private static final long serialVersionUID = 1L;

 private PlayerEnum selectedPlayerEnum;
 public static final String NAME_C = "Rafael Nadal";

 public PlayerEnum getSelectedPlayerEnum() {
  return selectedPlayerEnum;
 }

 public void setSelectedPlayerEnum(PlayerEnum selectedPlayerEnum) {
  this.selectedPlayerEnum = selectedPlayerEnum;
 }
}

Well, OmniFaces comes with a tag handler named, <o:importConstants/> that is capable to map of all constant field values of the given fully qualified name of a type in the request scope (it works with enums also). This tag handler is detailed in the book Mastering OmniFaces. PrimeFaces also comes with support for import constants and enums. Before PrimeFaces Elite 5.3.8 this support was available in PrimeFaces Extension as <pe:importConstants/> and <pe:importEnum/>.  Afterwards, this support was moved from PrimeFaces extension to PrimeFaces Elite 5.3.8, and is available via <p:importConstants/> and <p:importEnum/>. This tags are detailed in book, PrimeFaces & OmniFaces - Powers Combined.

Starting with JSF 2.3-m07, we can import constants and enums via the  new <f:importConstants/> tag. Check the NAME_C constant from above. Well, this constant (and any other constant from PlayerBean) can be accessed via EL once we import them as below (the type attribute is required and its value represents the fully qualified name of the class/interface/enum to import the constant field values for):

<f:importConstants type="javaee8.jsf23.PlayerBean" />

The <f:importConstants/> supports the var attribute also. You can use it to indicate an alias (the name of the request attribute which exposes the mapping of the constants in the request scope), as
below:

<f:importConstants type="javaee8.jsf23.PlayerBean" var="CONSTANTS" />

Now, we can write this: #{CONSTANTS.NAME_C}

The PlayerEnum can be imported exactly the same:

<f:importConstants type="javaee8.jsf23.PlayerEnum" />

This allows us to loop the enum, as below (returns FIRST SECOND THIRD):

<ui:repeat value="#{PlayerEnum.values()}" var="t">
 #{t}
</ui:repeat>

Now, we can nominate the enum constants:

#{PlayerEnum.FIRST} // returns FIRST
#{PlayerEnum.FIRST.rank} // returns 1

We can put all together and provide enum values as dropdown items:

<h:form>
 <h:selectOneMenu value="#{playerBean.selectedPlayerEnum}">
  <f:selectItems value="#{PlayerEnum}" />
  <f:ajax render="@form" />
 </h:selectOneMenu>
 #{playerBean.selectedPlayerEnum}
 [#{playerBean.selectedPlayerEnum == PlayerEnum.FIRST}]
</h:form>

Even more, we can provide the enum values as dropdown items via the var attribute:

<h:form>
 <h:selectOneMenu value="#{playerBean.selectedPlayerEnum}">
  <f:selectItems value="#{PlayerEnum.values()}" var="t" itemLabel="#{t.rank}" itemValue="#{t}" />
  <f:ajax render="@form" />
 </h:selectOneMenu>
 #{playerBean.selectedPlayerEnum}
 [#{playerBean.selectedPlayerEnum == PlayerEnum.FIRST}]
</h:form>

We can glue all the examples above in the below view:

<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:h="http://xmlns.jcp.org/jsf/html"
      xmlns:ui="http://xmlns.jcp.org/jsf/facelets"
      xmlns:f="http://xmlns.jcp.org/jsf/core">
   
 <f:metadata>
  <f:importConstants type="javaee8.jsf23.PlayerEnum" />
  <f:importConstants type="javaee8.jsf23.PlayerBean" />
  <f:importConstants type="javaee8.jsf23.PlayerBean" var="CONSTANTS" />
 </f:metadata>
 <h:head>
  <title>JSF 2.3 - ImportConstants</title>             
 </h:head>
 <h:body>                  
  <h5>Display the 'NAME_C' constant value</h5>
  <h:outputText value="#{PlayerBean.NAME_C}"/><br/>
  <h:outputText value="#{CONSTANTS.NAME_C}"/><br/>

  <h5>Loop enum values</h5>
  <ui:repeat value="#{PlayerEnum.values()}" var="t">
   #{t}
  </ui:repeat>

  <h5>Providing enum values as dropdown items and test again 'FIRST'</h5>
  <h:form>
   <h:selectOneMenu value="#{playerBean.selectedPlayerEnum}">       
    <f:selectItems value="#{PlayerEnum}" />
    <f:ajax render="@form" />
   </h:selectOneMenu>
   #{playerBean.selectedPlayerEnum}
   [#{playerBean.selectedPlayerEnum == PlayerEnum.FIRST}]
  </h:form>

  <h5>Providing enum values as dropdown items by var  and test again 'FIRST'</h5>
  <h:form>
   <h:selectOneMenu value="#{playerBean.selectedPlayerEnum}">       
    <f:selectItems value="#{PlayerEnum.values()}" var="t" itemLabel="#{t.rank}" itemValue="#{t}" />
    <f:ajax render="@form" />
   </h:selectOneMenu>
   #{playerBean.selectedPlayerEnum}
   [#{playerBean.selectedPlayerEnum == PlayerEnum.FIRST}]
  </h:form>
 </h:body>
</html>

The complete application is available here.

Niciun comentariu :

Trimiteți un comentariu

JSF BOOKS COLLECTION

Postări populare

Follow by Email

Visitors Starting 4 September 2015

Locations of Site Visitors