attribute I wrote a custom component for jsf. The renderer extends com.sun.faces.renderkit.html_...">

JSF custom component: how to get the <f: ajax / "> attribute

I wrote a custom component for jsf. The renderer extends com.sun.faces.renderkit.html_basic.ListboxRenderer. My component is located in "javax.faces.SelectMany" -Family.

The code in the jsf page is as follows:

<tb:myMenu id="testId" value="#{valueForm.someValue}"> <f:selectItem /> <f:selectItems value="#{dao.getSomething()}" /> <f:ajax render=":myTestForm:myId"/> </tb:myMenu> 

How can I get the render attribute value in my Renderer? I only need a value, nothing should be written for my component (for example, the RenderKitUtils class)

My current solution is shown below. It works, but I'm not happy with it.

 if (component instanceof ClientBehaviorHolder) { Map<String, List<ClientBehavior>> behaviors = ((ClientBehaviorHolder)component).getClientBehaviors(); if (behaviors != null && behaviors.keySet().contains("valueChange")) { for (ClientBehavior cb: behaviors.get("valueChange")) { if (cb instanceof AjaxBehavior) { System.out.println("AJAX: " + ((AjaxBehavior) cb).getRender()); } } } } 
+4
source share
1 answer

How do you dislike this? Too verbose? Well, in fact there is no utility method provided by the JSF API, or Mojarra impl, which hides this. He just stops here. You have to write it yourself.

At least in your snippet, a second if check for null is redundant because it never returns null . Further, behaviors.keySet().contains(key) on the same line can also be simplified to behaviors.containsKey(key) . Given the fact that it never returns null , you can also immediately get a list of actions and reset zero.

Finally, just hide it in some utility.

 public static Set<String> getClientBehaviorRenderIds(UIComponent component, String behaviorName) { Set<String> clientBehaviorRenderIds = new HashSet<String>(); if (component instanceof ClientBehaviorHolder) { List<ClientBehavior> clientBehaviors = ((ClientBehaviorHolder) component).getClientBehaviors().get(behaviorName); if (clientBehaviors != null) { for (ClientBehavior clientBehavior : clientBehaviors) { if (clientBehavior instanceof AjaxBehavior) { clientBehaviorRenderIds.addAll(((AjaxBehavior) clientBehavior).getRender()); } } } } return clientBehaviorRenderIds; } 

so you can use it like this:

 Set<String> renderIds = getClientBehaviorRenderIds(component, "valueChange"); // ... 

If these are nested checks that interfere, you can also do reverse checks (this is also how Mojarra is written in general, deep if nesting is really bad practice):

 public static Set<String> getClientBehaviorRenderIds(UIComponent component, String behaviorName) { Set<String> clientBehaviorRenderIds = new HashSet<String>(); if (!(component instanceof ClientBehaviorHolder)) { return clientBehaviorRenderIds; } List<ClientBehavior> clientBehaviors = ((ClientBehaviorHolder) component).getClientBehaviors().get(behaviorName); if (clientBehaviors == null) { return clientBehaviorRenderIds; } for (ClientBehavior clientBehavior : clientBehaviors) { if (clientBehavior instanceof AjaxBehavior) { clientBehaviorRenderIds.addAll(((AjaxBehavior) clientBehavior).getRender()); } } return clientBehaviorRenderIds; } 
+1
source

All Articles