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

duminică, 31 iulie 2016

[OmniFaces utilities (2.4)] Format the given number to nearest 10^n (rounded to thousands)


[OmniFaces utilities] The formatThousands() formats the given number to nearest 10n (rounded to thousands), immediately suffixed (without space) with metric unit (k, M, G, T, P or E), rounding half up with a precision of 3 digits, whereafter trailing zeroes in fraction part are stripped. Numbers lower than thousand are not affected. The format locale will be set to the one as obtained by Faces#getLocale(). If the value is null, NaN or infinity, then this will return null.

This function is available from OmniFaces 2.3, but in OmniFaces 2.4, the incorrectly trimmed trailing zeroes from non-fractional integer numbers was fixed and now also supports cutting down fractions of values lower than thousand.

Function:

Usage:

// 9.99 k
#{of:formatThousandsUnit(9994)}
      
// 10 M
#{of:formatThousandsUnit(9995000)}

// 532 k
#{of:formatThousandsUnit(532230.6483)}

sâmbătă, 23 iulie 2016

[OmniFaces utilities (2.4)] Format the given number to nearest 10^n, suffixed with a space, the metric unit prefix and the given unit


[OmniFaces utilities] The formatThousandsUnit() formats the given number to nearest 10n (rounded to thousands), suffixed with a space, the metric unit prefix (k, M, G, T, P or E) and the given unit, rounding half up with a precision of 3 digits, whereafter trailing zeroes in fraction part are stripped. The format locale will be set to the one as obtained by Faces#getLocale(). If the value is null, NaN or infinity, then this will return null.The given unit used in the format is of type B for Bytes, W for Watt, etc. If the unit is null, then this method will behave exactly as described in #formatThousands(Number).

This function is available from OmniFaces 2.3, but in OmniFaces 2.4, the incorrectly trimmed trailing zeroes from non-fractional integer numbers was fixed and now also supports cutting down fractions of values lower than thousand.

Function:

Usage:

// 9.99 kB
#{of:formatThousandsUnit(9994  , "B")}
      
// 10 MW
#{of:formatThousandsUnit(9995000 , "W")}
 
// 532 kFOO
#{of:formatThousandsUnit(532230.6483 , "FOO")}

duminică, 17 iulie 2016

[OmniFaces utilities (2.4)] Get all request query string or view parameters appended with all child UIParameter components of the given parent component


[OmniFaces utilities] The getParams() returns an unmodifiable map with all request query string or view parameters, appended with all child UIParameter components (<f|o:param>) of the given parent component. Those with disabled=true or an empty name or an empty value are skipped. The <f|o:param> will override any included view or request parameters on the same name.

Method:
Usage:

Let's suppose that we have the following page:

<f:metadata>
 <f:viewParam name="playernameparam" value="#{playersBean.playerName}"/>           
 <f:viewParam name="playersurnameparam" value="#{playersBean.playerSurname}"/>
</f:metadata>
<h:head>
 <title></title>
</h:head>  
<h:body>    
 <h:panelGrid columns="2">
  <h:form id="form">
   <h:commandButton id="submit" value="Get all params" action="#{playersBean.allParams()}">   
    <f:param name="ageParam" value="29"/>
    <o:param name="playParam" value="left"/>
   </h:commandButton>
  </h:form>                 
 </h:panelGrid>
</h:body>

Further, we can use the Components#getParams() utility to programmatically collect the UIParameters  (<f|o:param/>) nested in the above <h:commandButton/> and the view parameters. This can be accomplish in different JSF artifacts; for example, in a bean (obviously, there are several ways to identify the parent UIComponent of the UIParameters that we want to collect - the UIComponent that should be passed to the #getParams() should be obtained in a convenient manner depending on your circumstances; below, we simply used the Components#findComponent() utility):

import org.omnifaces.util.Components;
...
public void allParams() {
              
 UIComponent uc = Components.findComponent("form:submit");
 Map<String, List<String>> paramsMap = Components.getParams(uc, false, true);

 for (Map.Entry<String, List<String>> entry : paramsMap.entrySet()) {
      System.out.println("Param: "+ entry.getKey() + "/" + entry.getValue());
 }
}

Let's suppose that we start the application with an URL like this:

http://myapp/?playernameparam=rafael&playersurnameparam=nadal

After we press the Get all params button the server log will reveal the following output:

Param: playernameparam/[rafael]
Param: playersurnameparam/[nadal]
Param: ageParam/[29]
Param: playParam/[left]

sâmbătă, 9 iulie 2016

[OmniFaces utilities (2.4)] Get the HTTP request URL with query string, regardless of any forward


[OmniFaces utilities] The getRequestURL() method returns the HTTP request URL with query string, regardless of any forward. This is the full request URL without query string as the enduser sees in browser address bar.

Method:

Usage:

import org.omnifaces.util.Servlets;
...
@WebServlet("/MyServlet")
public class MyServlet extends HttpServlet {
 protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  // e.g. http://localhost:8080/MyApp/MyServlet
  String req = Servlets.getRequestURL(request);
 }
 ...
}

miercuri, 6 iulie 2016

[OmniFaces utilities (2.4)] Get the HTTP request query string, regardless of any forward


[OmniFaces utilities] The getRequestQueryString() method returns the HTTP request query string, regardless of any forward.

Method:
Use this one outside JSF context
(Servlets#getRequestQueryString())
See also: Utils.coalesce()
Use this one in JSF context
(Faces#getRequestQueryString())
See also:  Faces.getRequest() | Faces.getContext()
Usage:

Some use cases are available right in OmniFaces source code. For example, OmniFaces uses getRequestQueryString() to get the HTTP request URI with query string, regardless of any forward:

public static String getRequestURIWithQueryString(HttpServletRequest request) {
 String requestURI = getRequestURI(request);
 String queryString = getRequestQueryString(request);
 return (queryString == null) ? requestURI : (requestURI + "?" + queryString);
}

luni, 4 iulie 2016

[OmniFaces utilities (2.4)] Get the HTTP request URI, regardless of any forward or error dispatch


[OmniFaces utilities] The getRequestURI() method returns the HTTP request URI, regardless of any forward or error dispatch. This is the part after the domain in the request URL, including the leading slash.

Method:
Use this one in JSF context
(Faces#getRequestURI())
See also:  Faces.getRequest()  Faces.getContext()
Use this one outside JSF context
(Servlets#getRequestURI())
See also: Utils.coalesce()
                              
Usage:

Some use cases are available right in OmniFaces source code. For example, OmniFaces uses getRequestURI() to check if the given HTTP servlet request is a JSF resource request:

public static boolean isFacesResourceRequest(HttpServletRequest request) {
 return getRequestURI(request).startsWith(request.getContextPath() + ResourceHandler.RESOURCE_IDENTIFIER + "/");
}


public static String getRequestURIWithQueryString(HttpServletRequest request) {
 String requestURI = getRequestURI(request);
 String queryString = getRequestQueryString(request);
 return (queryString == null) ? requestURI : (requestURI + "?" + queryString);
}

duminică, 3 iulie 2016

[OmniFaces utilities (2.4)] Get a list of all action expressions and listeners associated with given component


[OmniFaces utilities] The getActionExpressionsAndListeners() method returns a list of all action expressions and listeners associated with given component. This covers expressions in action attribute of command components and listener attribute of ajax components. Any method expressions are in format #{bean.method} and any action listeners are added as fully qualified class names. This list is primarily useful for logging postback actions in a phase listener. You can use #getCurrentActionSource() to obtain the current action source.

Method:

Usage:

Let's suppose the following code:

<h:form>
 <h:commandButton value="Foo" actionListener="#{fooBean.fooActionListener()}" action="#{fooBean.fooAction()}">
  <f:setPropertyActionListener id="foo" target="#{fooBean.foo}" value="Foo"/>                              
  <f:ajax execute="@form" render="@form" listener="#{fooBean.ajaxFooListener()}"/>
 </h:commandButton>
</h:form>

Now, programmatically (for example in a phase listener) we can do this:

import org.omnifaces.util.Components;
...
// use getCurrentActionSource() to get the source of the currently invoked action
UIComponent command = Components.getCurrentActionSource();
List<String> ael = Components.getActionExpressionsAndListeners(command);

// or, use getCurrentCommand() to get the currently invoked UI command component
UIComponent command = Components.getCurrentCommand();
List<String> ael = Components.getActionExpressionsAndListeners(command);

Both examples will return a List<String>  that contains the following items:

[
 #{fooBean.fooAction()},
 javax.faces.event.MethodExpressionActionListener,  
 com.sun.faces.facelets.tag.jsf.core.SetPropertyActionListenerHandler$SetPropertyListener,
 #{fooBean.ajaxFooListener()}
]

[OmniFaces utilities (2.4)] Get the source of the currently invoked action


[OmniFaces utilities] The getCurrentActionSource() method returns the source of the currently invoked action, or null if there is none, which may happen when the current request is not a postback request at all, or when the view has been changed by for example a successful navigation. If the latter is the case, you'd better invoke this method before navigation.

Method:
Usage:

import org.omnifaces.util.Components;
...
UIComponent action = Components.getCurrentActionSource();

You can see several examples of using this method as:

- check whether the given component has invoked the form submit (hasInvokedSubmit()):

public static boolean hasInvokedSubmit(UIComponent component) {
 UIComponent source = getCurrentActionSource();
 return source != null && source.equals(component);
}

- get the currently invoked UI command component (getCurrentCommand()):

public static UICommand getCurrentCommand() {
 UIComponent source = getCurrentActionSource();
 return source instanceof UICommand ? (UICommand) source : null;
}

JSF BOOKS COLLECTION

Postări populare

Visitors Starting 4 September 2015

Locations of Site Visitors