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
-
答案 0 :(得分:2)
并不是因为扩展类使得它不能用于注入,因为@FacesBehaviour
注释不会使它成为注入目标。一个小小的谷歌搜索告诉我:In JSF 2.2 it is not a target for injection, in 2.3 it is。
OmniFaces在FacesConverter和FacesValidator中添加了对注入的支持,以便与jsf 2.2一起使用,但不支持FacesBehaviour。
另见: