http://jsfcorner.blogspot.jp/2013/02/client-behavior-functionality-with-jsf.html
を参考にBehaviorを実装してみたがbehavior-classが見つからないエラーが発生してしまう。
エラー
javax.faces.FacesException: Could not find any registered behavior-class for behaviorId : confirmation
at org.apache.myfaces.application.ApplicationImpl.createBehavior(ApplicationImpl.java:1182)
at javax.faces.application.ApplicationWrapper.createBehavior(ApplicationWrapper.java:113)
at org.apache.myfaces.view.facelets.tag.jsf.BehaviorTagHandlerDelegate.createBehavior(BehaviorTagHandlerDelegate.java:102)
at org.apache.myfaces.view.facelets.tag.jsf.BehaviorTagHandlerDelegate.applyAttachedObject(BehaviorTagHandlerDelegate.java:137)
at org.apache.myfaces.view.facelets.tag.jsf.BehaviorTagHandlerDelegate.apply(BehaviorTagHandlerDelegate.java:71)
以下は上記サイトを参考に実装した内容。
ConfirmBehavior.java
@FacesBehavior(value = "confirmation")
public class ConfirmBehavior extends ClientBehaviorBase {
@Override
public String getScript(ClientBehaviorContext behaviorContext) {
return "return confirm('Are you sure ?');";
}
}
taglib.xml
<namespace>http://www.rubus.be/jsf/tutorial</namespace>
<tag>
<tag-name>confirmation</tag-name>
<behavior>
<behavior-id>confirmation</behavior-id>
</behavior>
</tag>
confirm.xhtml
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:r="http://www.rubus.be/jsf/tutorial"
...
<h:commandButton value="with confirmation" actionListener="#{bean.performAction}">
<r:confirmation/>
</h:commandButton>
...
</html>
ConfirmBehavior.javaがbehaiviorとして読み込まれていないのが原因のようだったので、faces-config.xmlに追加してみたらうまくいった。
faces-config.xml
<behavior>
<behavior-id>confirmation</behavior-id>
<behavior-class>xxx.ConfirmBehavior</behavior-class>
</behavior>