JSF 2 setPropertyActionListenerの例
JSFでは、 ”
f:setPropertyActionListener
“タグを使用して、バッキングBeanのプロパティに直接値を設定できます。例えば、
<h:commandButton action="#{user.outcome}" value="Submit">
<f:setPropertyActionListener target="#{user.username}" value="mkyong"/>
</h:commandButton>
上記のJSFコードスニペットで、ボタンをクリックすると、
setUsername()
メソッドを介して ”
mynong
“値を ”
username
“プロパティに設定します。
@ManagedBean(name="user")
@SessionScoped
public class UserBean{
public String username;
public void setUsername(String username) {
this.username = username;
}
}
JSF f:setPropertyActionListenerの例
さて、JSF 2.0の完全な例を見てみましょう。
1.マネージドBean
“user”というスーパーシンプルマネージドBean。
package com.mkyong;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
@ManagedBean(name="user")
@SessionScoped
public class UserBean{
public String username;
public String outcome(){
return "result";
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
}
2. JSFページ
JSFページを使用して、 ”
f:setPropertyActionListener
“を使用して、バッキングBeanのプロパティ ”
username
“に値 ”
mkyong
“を直接設定します。
default.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:body>
<h1>JSF 2 setPropertyActionListener example</h1>
<h:form id="form">
<h:commandButton action="#{user.outcome}" value="Click Me">
<f:setPropertyActionListener target="#{user.username}" value="mkyong"/>
</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:h="http://java.sun.com/jsf/html"
>
<h:body>
<h1>JSF 2 setPropertyActionListener example</h1>
#{user.username}
</h:body>
</html>
デモ
ボタンをクリックした後の結果です。

ソースコードをダウンロードする
ダウンロード – リンク://wp-content/uploads/2010/11/JSF-2-setPropertyActionListener-Example.zip[JSF-2-setPropertyActionListener-Example.zip](9KB)
リファレンス
2 setPropertyActionListener JavaDoc]
リンク://タグ/jsf2/[jsf2]