Java – JSF custom component: how to get properties

I wrote a custom component for JSF The renderer extends com sun. faces. renderkit. html_ basic. List@R_776_2419 @Renderer. My component is located in "javax. Faces. Selectmany" – home

The code in 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 do I get the value of render attribute in the renderer? I only need values and should not write to my components (like renderkitutils class)

My current solution is as follows It works, but I'm not satisfied 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());
                }
            }
        }
    }

Solution

Are you not satisfied with this? Too long? Well, there are no practical methods provided by JSF API and mojarra impl It stops right here Write it yourself

At least, in your code snippet, the second check for null is redundant because it never returns null In addition, behavior keySet(). Contains (key) on the same line can also be simplified to behavior containsKey(key). Since it never returns null, you can also get the behavior list immediately and recheck it

Finally, some practical methods are used to hide it

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 as follows:

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

If nested checking is disturbing, you can also perform reverse checking (this is the way mojarra usually writes; if nesting is really a bad practice, then:

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;
}
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>