Jsf-actionlistener-tag
提供:Dev Guides
JSF-actionListener
ユーザーがh:commandButtonやh:linkなどのコンポーネントを操作すると、JSFは2つの方法で処理できるアクションイベントを発生させます。
S.No | Technique & Description |
---|---|
1 |
Method Binding UIコンポーネントの_actionListener_属性でマネージドBeanメソッドの名前を渡します。 |
2 |
ActionListener ActionListenerインターフェースを実装し、実装クラス名をUIコンポーネントの_actionListener_属性に渡します。 |
メソッドのバインド
メソッドを定義する
public void updateData(ActionEvent e) {
data = "Hello World";
}
上記の方法を使用してください
<h:commandButton id = "submitButton"
value = "Submit" action = "#{userData.showResult}"
actionListener = "#{userData.updateData}"/>
</h:commandButton>
ActionListener
ActionListenerを実装する
public class UserActionListener implements ActionListener {
@Override
public void processAction(ActionEvent arg0)
throws AbortProcessingException {
//access userData bean directly
UserData userData = (UserData) FacesContext.getCurrentInstance().
getExternalContext().getSessionMap().get("userData");
userData.setData("Hello World");
}
}
リスナーメソッドを使用する
<h:commandButton id = "submitButton1"
value = "Submit" action = "#{userData.showResult}" >
<f:actionListener type = "com.finddevguides.test.UserActionListener"/>
</h:commandButton>
応用例
テストJSFアプリケーションを作成して、JSFのactionListenerをテストしましょう。
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 UserActionListener.java file under a package com.finddevguides.test. Modify it as explained below. |
4 | Modify home.xhtml as explained below. Keep the rest of the files unchanged. |
5 | Modify result.xhtml 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 java.util.LinkedHashMap;
import java.util.Map;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.event.ValueChangeEvent;
@ManagedBean(name = "userData", eager = true)
@SessionScoped
public class UserData implements Serializable {
private static final long serialVersionUID = 1L;
private static Map<String,String> countryMap;
private String data = "sample data";
public String showResult() {
return "result";
}
public void updateData(ActionEvent e) {
data="Hello World";
}
public String getData() {
return data;
}
public void setData(String data) {
this.data = data;
}
}
UserActionListener.java
package com.finddevguides.test;
import javax.faces.context.FacesContext;
import javax.faces.event.AbortProcessingException;
import javax.faces.event.ActionEvent;
import javax.faces.event.ActionListener;
public class UserActionListener implements ActionListener {
@Override
public void processAction(ActionEvent arg0)
throws AbortProcessingException {
//access userData bean directly
UserData userData = (UserData) FacesContext.getCurrentInstance().
getExternalContext().getSessionMap().get("userData");
userData.setData("Hello World");
}
}
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>actionListener Examples</h2>
<h:form>
<h2>Method Binding</h2>
<hr/>
<h:commandButton id = "submitButton"
value = "Submit" action = "#{userData.showResult}"
actionListener = "#{userData.updateData}"/>
</h:commandButton>
<h2>ActionListener interface</h2>
<hr/>
<h:commandButton id = "submitButton1"
value = "Submit" action = "#{userData.showResult}" >
<f:actionListener
type = "com.finddevguides.test.UserActionListener"/>
</h:commandButton>
</h:form>
</h:body>
</html>
result.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:f = "http://java.sun.com/jsf/core"
xmlns:h = "http://java.sun.com/jsf/html">
<h:head>
<title>JSF Tutorial!</title>
</h:head>
<h:body>
<h2>Result</h2>
<hr/>
#{userData.data}
</h:body>
</html>
すべての変更を完了したら、JSF-最初のアプリケーションの章で行ったようにアプリケーションをコンパイルして実行します。 すべてがアプリケーションで問題ない場合、次の結果が生成されます。
送信ボタンをクリックします。 次の結果が表示されます。