Search code examples
jsf-2.2mojarraglassfish-4.1.1

Is the class extending ClientBehaviorBase can be considered as an injection target?


The bean is defined to be as simple as follows:

@Named
@RequestScoped
public class ConfirmBean {

    private String confirmMsg;

    public ConfirmBean(){
        confirmMsg = "Are you sure you want to delete this file ?";
    }   

   // getters & setters           
}

And the class inheriting the ClientBehaviorBase

@FacesBehavior(value = "confirm")
public class ConfirmDeleteBehavior extends ClientBehaviorBase {

    @Inject
    ConfirmBean confirmBean;
    //@Inject
    //ConfirmEJBBean confirmEJBBean;
    //@EJB
    //ConfirmEJBBean confirmEJBBean;

    @Override
    public String getScript(ClientBehaviorContext behaviorContext) {
        return "return confirm('"+confirmBean.getConfirmMsg()+"');";
    }
}

with the taglib file-

    <namespace>http://www.custom.tags/jsf/delete</namespace>
    <tag>
        <tag-name>confirmDelete</tag-name>
        <behavior>
            <behavior-id>confirm</behavior-id>
        </behavior>
    </tag>

with an entry as context-param in web.xml-

<context-param>
    <param-name>javax.faces.FACELETS_LIBRARIES</param-name>
    <param-value>/WEB-INF/taglib/delete.taglib.xml</param-value>
</context-param>

and accessing this custom tag in the front layer-

        <h:form>
            <h:commandButton value="Delete" action="done">
                <b:confirmDelete/>
            </h:commandButton>
        </h:form>

I am using Glassfish application server 4.1.1 with Mojarra 2.2.12 version being used internally.

I got a NPE here-

enter image description here


Solution

  • It's not so much that the extending the class makes it not eligable for injection it is that the @FacesBehaviour is annotation does not make it a target of injection. A little googling told me this: In JSF 2.2 it is not a target for injection, in 2.3 it is.

    OmniFaces added support for injection in FacesConverters and FacesValidators to be used with jsf 2.2 but not the FacesBehaviour.

    See also: