miercuri, 21 ianuarie 2015

OmniFaces 2.0 - Write a custom component that receive the FaceletContext for the Facelet in which it is declared

Derived from EL context, the FaceletContext is the context representative of a single request from a Facelet. Sometimes you may need this context to be available in your custom component. Per example, you can use FaceletContext to store a ValueExpression in the "facelet scope". FaceletContext has access to the EL variable mapper via getVariableMapper()/setVariableMapper(). An EL variable mapper provides a method for resolving ValueExpressions by name, resolveVariable(), and one method for assigning a ValueExpressions to an EL variable, setVariable():
...
//put a ValueExpression in the "facelet" scope
FaceletContext faceletContext;
faceletContext.getVariableMapper().setVariable("variable_name", value_expression);
...
//get a ValueExpression from "facelet" scope
FaceletContext faceletContext;
ValueExpression ve = faceletContext.getVariableMapper().getVariable("variable_name");
...
But, how to obtain the FaceletContext inside a custom component ? Well, OmniFaces knows how! Whenever you need the FaceletContext for the Facelet in which your own components are declared, you can use the OmniFaces solution, which is available if you respect the following two simple bullets:

·         your custom component

@FacesComponent(...)
public class YourComponent implements FaceletContextConsumer {

  @Override
  public void setFaceletContext(FaceletContext faceletContext) {
    // use the faceletContext to do something
  }
}

·         your *.taglib file:
...
<tag>
  ...
  <tag-name>your tag name for your component</tag-name>
    <component>
      <component-type>your component  type</component-type>
      <handler-class>org.omnifaces.taghandler.ComponentExtraHandler</handler-class>
    </component>
  ...
</tag>

Niciun comentariu:

Trimiteți un comentariu