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

joi, 2 iulie 2015

[OmniFaces utilities 2.1] Get the value of the given tag attribute as a value expression


[OmniFaces utilities] The getValueExpression() method returns the value of the given tag attribute as a value expression, so it can be carried around and evaluated at a later moment in the lifecycle without needing the Facelet context.

Method:
Usage:

The Facelets#getValueExpression() returns the value of the given tag attribute as a value expression, so it can be carried around and evaluated at a later moment in the lifecycle without needing the Facelet context. For example, the Facelet context is available in a tag handler when the component tree is build, but you may not need the values of tag attributes (e.g. attr1 and attr2) at that moment, you may need them later, for example after the validation take place (at PostValidateEvent). The problem is that the Facelet context is not available then, so you cannot simply call, FaceletContext#getAttribute() method. So, when the Facelet context is available, we can collect the value expressions of those attributes as below:

public class MyTagHander extends TagHandler {

 private ValueExpression veAttr1;
 private ValueExpression veAttr2;

 @Override
 public void apply(FaceletContext context, final UIComponent parent) throws IOException {

  veAttr1 = Facelets.getValueExpression(context, getAttribute("attr1"), String.class);
  veAttr2 = Facelets.getValueExpression(context, getAttribute("attr2"), String.class); 

  // for example, here you can subscribe to PostValidateEvent
 }
}

Later, at PostValidateEvent, we can evaluate veAttr1 and veAttr2, as below:

FacesContext context = ...;
ELContext elContext = context.getELContext();
String evaluatedAttr1 = evaluate(elContext, veAttr1, true);
String evaluatedAttr2 = evaluate(elContext, veAttr2, true);

private static String evaluate(ELContext context, ValueExpression expression, boolean required) {
 Object value = (expression != null) ? expression.getValue(context) : null;

 if (required && isEmpty(value)) { // see Utils#isEmpty()
     throw new IllegalArgumentException(...);
 }

 return (value != null) ? value.toString() : null;
}

You can see a complete example of using Facelets#getValueExpression() in OmniFaces ViewParamValidationFailed.

Niciun comentariu :

Trimiteți un comentariu

JSF BOOKS COLLECTION

Postări populare

OmniFaces/JSF Fans

Visitors Starting 4 September 2015

Locations of Site Visitors