扩展ClientBehaviorBase的类是否可以被视为注入目标?

时间:2016-06-14 16:11:55

标签: jsf-2.2 mojarra glassfish-4.1.1

bean定义如下:

@Named
@RequestScoped
public class ConfirmBean {

    private String confirmMsg;

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

   // getters & setters           
}

继承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()+"');";
    }
}

使用taglib文件 -

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

context-param -

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

并在前层访问此自定义标记 -

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

我正在使用内部使用Glassfish版本的4.1.1应用服务器Mojarra 2.2.12

我在这里得到NPE -

enter image description here

1 个答案:

答案 0 :(得分:2)

并不是因为扩展类使得它不能用于注入,因为@FacesBehaviour注释不会使它成为注入目标。一个小小的谷歌搜索告诉我:In JSF 2.2 it is not a target for injection, in 2.3 it is

OmniFaces在FacesConverterFacesValidator中添加了对注入的支持,以便与jsf 2.2一起使用,但不支持FacesBehaviour。

另见: