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


miercuri, 8 iunie 2016

Generic client behavior example

Before checking this application, please get familiar with client behaviors. A simple example is available here.

Now, let's write a generic client behavior example. We simply want to display an image, and react via a JS snippet of code to the following events that occur on this image: onclick, onmouseout, onmouseover, onmousedown and onmouseup.

1. The index.xhtml looks like this:

 <h:panelGrid columns="2">
  <h:graphicImage library="default" name="pfof.png">
  <div id="behaviorsId" style="width:300px;"/>

2. We use a tag handler to programmatically register behaviors:

public class GenericBehaviorTagHandler extends TagHandler {

 private final GenericBehaviorHandler onmouseout = new GenericBehaviorHandler();
 private final GenericBehaviorHandler onmouseover = new GenericBehaviorHandler();
 private final GenericBehaviorHandler onclick = new GenericBehaviorHandler();
 private final GenericBehaviorHandler onmousedown = new GenericBehaviorHandler();
 private final GenericBehaviorHandler onmouseup = new GenericBehaviorHandler();

 public GenericBehaviorTagHandler(TagConfig tagConfig) {

 public void apply(FaceletContext ctx, UIComponent parent) throws IOException {

  if (parent instanceof ClientBehaviorHolder) {
      ClientBehaviorHolder clientBehaviorHolder = (ClientBehaviorHolder) parent;

      Map<String, List<ClientBehavior>> behaviors = clientBehaviorHolder.getClientBehaviors();

      if (!(behaviors.containsKey("mouseout"))
          && !(behaviors.containsKey("click"))
          && !(behaviors.containsKey("mouseover"))
          && !(behaviors.containsKey("mousedown"))
          && !(behaviors.containsKey("mouseup"))) {

           clientBehaviorHolder.addClientBehavior("mouseout", onmouseout);
           clientBehaviorHolder.addClientBehavior("mouseover", onmouseover);
           clientBehaviorHolder.addClientBehavior("click", onclick);
           clientBehaviorHolder.addClientBehavior("mousedown", onmousedown);
           clientBehaviorHolder.addClientBehavior("mouseup", onmouseup);

3. The behaviors are instances of GenericBehaviorHandler:

public class GenericBehaviorHandler extends ClientBehaviorBase {

 public String getRendererType() {          
  return "genericbehaviorrenderer";

4. And the behavior renderer is:

@FacesBehaviorRenderer(rendererType = "genericbehaviorrenderer")
public class GenericBehaviorRenderer extends ClientBehaviorRenderer {

 public String getScript(ClientBehaviorContext behaviorContext, ClientBehavior behavior) {
  return "document.getElementById('behaviorsId').innerHTML += '" + behaviorContext.getEventName() + " |';";

The complete example is available here.

Niciun comentariu :

Trimiteți un comentariu


Postări populare

Follow by Email

Visitors Starting 4 September 2015

Locations of Site Visitors