Jsf-applicationevents-tag
JSF-アプリケーションイベント
JSFは、JSFアプリケーションライフサイクル中にアプリケーション固有のタスクを実行するシステムイベントリスナーを提供します。
S.No | System Event & Description |
---|---|
1 |
PostConstructApplicationEvent アプリケーションの起動時に起動します。 アプリケーションの起動後に初期化タスクを実行するために使用できます。 |
2 |
PreDestroyApplicationEvent アプリケーションがシャットダウンしようとしているときに起動します。 アプリケーションをシャットダウンする前に、クリーンアップタスクを実行するために使用できます。 |
3 |
PreRenderViewEvent JSFページが表示される前に発生します。 ユーザーを認証し、JSFビューへの制限付きアクセスを提供するために使用できます。 |
システムイベントは、次の方法で処理できます。
S.No | Technique & Description |
---|---|
1 |
SystemEventListener SystemEventListenerインターフェイスを実装し、faces-config.xmlにsystem-event-listenerクラスを登録します |
2 |
Method Binding マネージドBeanメソッドの名前をf:eventの_listener_属性に渡します。 |
SystemEventListener
SystemEventListenerインターフェイスを実装します。
public class CustomSystemEventListener implements SystemEventListener {
@Override
public void processEvent(SystemEvent event) throws
AbortProcessingException {
if(event instanceof PostConstructApplicationEvent) {
System.out.println("Application Started.
PostConstructApplicationEvent occurred!");
}
}
}
システムイベントのカスタムシステムイベントリスナーをfaces-config.xmlに登録します。
<system-event-listener>
<system-event-listener-class>
com.finddevguides.test.CustomSystemEventListener
</system-event-listener-class>
<system-event-class>
javax.faces.event.PostConstructApplicationEvent
</system-event-class>
</system-event-listener>
メソッドのバインド
メソッドを定義する
public void handleEvent(ComponentSystemEvent event) {
data = "Hello World";
}
上記の方法を使用します。
<f:event listener = "#{user.handleEvent}" type = "preRenderView"/>
応用例
テストJSFアプリケーションを作成して、JSFのシステムイベントをテストしましょう。
Step | Description |
---|---|
1 | Create a project with a name helloworld under a package com.finddevguides.test as explained in the JSF - First Application chapter. |
2 | Modify UserData.java file as explained below. |
3 | Create CustomSystemEventListener.java file under a package com.finddevguides.test. Modify it as explained below |
4 | Modify home.xhtml as explained below. |
5 | Create faces-config.xml in WEB-INF folder.Modify it as explained below. Keep the rest of the files unchanged. |
6 | Compile and run the application to make sure the business logic is working as per the requirements. |
7 | Finally, build the application in the form of war file and deploy it in Apache Tomcat Webserver. |
8 | Launch your web application using appropriate URL as explained below in the last step. |
UserData.java
package com.finddevguides.test;
import java.io.Serializable;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.event.ComponentSystemEvent;
@ManagedBean(name = "userData", eager = true)
@SessionScoped
public class UserData implements Serializable {
private static final long serialVersionUID = 1L;
private String data = "sample data";
public void handleEvent(ComponentSystemEvent event) {
data = "Hello World";
}
public String getData() {
return data;
}
public void setData(String data) {
this.data = data;
}
}
CustomSystemEventListener.java
package com.finddevguides.test;
import javax.faces.application.Application;
import javax.faces.event.AbortProcessingException;
import javax.faces.event.PostConstructApplicationEvent;
import javax.faces.event.PreDestroyApplicationEvent;
import javax.faces.event.SystemEvent;
import javax.faces.event.SystemEventListener;
public class CustomSystemEventListener implements SystemEventListener {
@Override
public boolean isListenerForSource(Object value) {
//only for Application
return (value instanceof Application);
}
@Override
public void processEvent(SystemEvent event)
throws AbortProcessingException {
if(event instanceof PostConstructApplicationEvent) {
System.out.println("Application Started.
PostConstructApplicationEvent occurred!");
}
if(event instanceof PreDestroyApplicationEvent) {
System.out.println("PreDestroyApplicationEvent occurred.
Application is stopping.");
}
}
}
home.xhtml
<?xml version = "1.0" encoding = "UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns = "http://www.w3.org/1999/xhtml"
xmlns:h = "http://java.sun.com/jsf/html"
xmlns:f = "http://java.sun.com/jsf/core">
<h:head>
<title>JSF tutorial</title>
</h:head>
<h:body>
<h2>Application Events Examples</h2>
<f:event listener = "#{userData.handleEvent}" type = "preRenderView"/>
#{userData.data}
</h:body>
</html>
faces-config.xhtml
<?xml version = "1.0" encoding = "UTF-8"?>
<faces-config
xmlns = "http://java.sun.com/xml/ns/javaee"
xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation = "http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-facesconfig_2_0.xsd"
version = "2.0">
<application>
<!-- Application Startup -->
<system-event-listener>
<system-event-listener-class>
com.finddevguides.test.CustomSystemEventListener
</system-event-listener-class>
<system-event-class>
javax.faces.event.PostConstructApplicationEvent
</system-event-class>
</system-event-listener>
<!-- Before Application is to shut down -->
<system-event-listener>
<system-event-listener-class>
com.finddevguides.test.CustomSystemEventListener
</system-event-listener-class>
<system-event-class>
javax.faces.event.PreDestroyApplicationEvent
</system-event-class>
</system-event-listener>
</application>
</faces-config>
すべての変更を完了したら、JSF-最初のアプリケーションの章で行ったようにアプリケーションをコンパイルして実行します。 すべてがアプリケーションで問題ない場合、次の結果が生成されます。
Webサーバーコンソールの出力を調べます。 次の結果が表示されます。
INFO: Deploying web application archive helloworld.war
Dec 6, 2012 8:21:44 AM com.sun.faces.config.ConfigureListener contextInitialized
INFO: Initializing Mojarra 2.1.7 (SNAPSHOT 20120206) for context '/helloworld'
Application Started. PostConstructApplicationEvent occurred!
Dec 6, 2012 8:21:46 AM com.sun.faces.config.ConfigureListener
$WebConfigResourceMonitor$Monitor <init>
INFO: Monitoring jndi:/localhost/helloworld/WEB-INF/faces-config.xml
for modifications
Dec 6, 2012 8:21:46 AM org.apache.coyote.http11.Http11Protocol start
INFO: Starting Coyote HTTP/1.1 on http-8080
Dec 6, 2012 8:21:46 AM org.apache.jk.common.ChannelSocket init
INFO: JK: ajp13 listening on/0.0.0.0:8009
Dec 6, 2012 8:21:46 AM org.apache.jk.server.JkMain start
INFO: Jk running ID = 0 time = 0/24 config = null
Dec 6, 2012 8:21:46 AM org.apache.catalina.startup.Catalina start
INFO: Server startup in 44272 ms