JSFページからバッキングBeanにパラメータを渡す4つの方法
私が知っているように、JSFページからバッキングBeanへのパラメータ値を渡す4つの方法があります:
-
メソッド式(JSF 2.0)
-
f:パラメータ
-
f:属性
-
f:setPropertyActionListener
例をひとつずつ見てみましょう:
1.メソッド式
JSF 2.0以降、
#\ {bean.method(param)}
のようにメソッドの式にパラメータ値を渡すことができます。
__JSFページ…
<h:commandButton action="#{user.editAction(delete)}"/>
Backing bean …
@ManagedBean(name="user")
@SessionScoped
public class UserBean{
public String editAction(String id) {
//id = "delete"
}
}
2. f:param
`f:param`タグを介してパラメータ値を渡し、バッキングBeanのrequestパラメータを介して取得します。
__JSFページ…
<h:commandButton action="#{user.editAction}">
<f:param name="action" value="delete"/>
</h:commandButton>
Backing bean …
@ManagedBean(name="user")
@SessionScoped
public class UserBean{
public String editAction() {
Map<String,String> params =
FacesContext.getExternalContext().getRequestParameterMap();
String action = params.get("action");
//...
}
}
ここでは、//jsf2/jsf-2-param-example/[f:paramの例]のフルリンクを参照してください。
3. f:アトリビュート
`f:atribute`タグを介してパラメータ値を渡し、バッキングBeanのアクションリスナを介して戻します。
__JSFページ…
<h:commandButton action="#{user.editAction}" actionListener="#{user.attrListener}">
<f:attribute name="action" value="delete"/>
</h:commandButton>
Backing bean …
@ManagedBean(name="user")
@SessionScoped
public class UserBean{
String action;
//action listener event
public void attrListener(ActionEvent event){
action = (String)event.getComponent().getAttributes().get("action");
}
public String editAction() {
//...
}
}
4. f:setPropertyActionListener
`f:setPropertyActionListener`タグを介してパラメータ値を渡すと、その値をあなたのバッキングBeanプロパティに直接設定します。
__JSFページ…
<h:commandButton action="#{user.editAction}" >
<f:setPropertyActionListener target="#{user.action}" value="delete"/>
</h:commandButton>
Backing bean …
@ManagedBean(name="user")
@SessionScoped
public class UserBean{
public String action;
public void setAction(String action) {
this.action = action;
}
public String editAction() {
//now action property contains "delete"
}
}
P.S他の方法があれば、あなたのアイデアをお伝えください:)