Clone wiki

AspectFaces / 7.1.Aspect-Faces-Context

Aspect Faces has its own context which is published into JSF context. Those variables are then accessible in UI widget templates and mapping rules and you are free to use them for decision making and UI transformation, as is shown in 4. Type Mapping and 5. UI Widgets. Here we present a way how to manually expose some variables into this context. Another way, the automatic is presented in chapter 7. Model Annotations.

First of all, you need to override class DefaultAFGeneratorHandler handling tags af:ui responsible for rendering components and aspect weaving. It contains method hookAddToAFContext invoked before component processing and it allows to publish your own variables.

Information passing to the context through the JSFHandler

public class CustomHandler extends DefaultAFGeneratorHandler {

  @Override
  protected void hookAddToAFContext(Context context) {

    // publish list of current UI profiles
    context.setProfiles(new String[]{"US"});

    // public list of current user's security roles
    context.setRoles(new String[]{"Admin"}); 

    // set layout to use for rendering
    context.setLayout("specialLayout.xhtml"); 

    // custom constant in JSF context
    context.getVariables().put("customVariable", "Value of the  variable"); 

    // extension of EL by custom class with util functions
    context.getVariables().put("utils", new Utils()); 
  }
}

Next, to make it work, we need to replace default handler class in the taglib by your own. Open the file /WEB-INF/aspectfaces.taglib.xml and change it to the following configuration:

Custom Handler Registration

<tag>
  <tag-name>ui</tag-name>
  <component>
    <component-type>ui</component-type>
    <renderer-type>uigen.renderer</renderer-type>
    <handler-class>our.custom.handler.CustomHandler</handler-class>
  </component>
</tag>

This kind of variable injection into AF context is largely used in following chapters to set current execution context because a lot of configuration might be derived from multiple different services such as authentication framework, geographical location detection, type of action detection and so on. For more details please keep reading following chapters.

Updated