[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 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