JSF 2のparamの例

JSF 2パラメーターの例

JSFでは、「f:param」タグを使用してパラメータをコンポーネントに渡すことができますが、その動作は、接続されているコンポーネントのタイプによって異なります。 例えば、

1. f:param + h:outputFormat

f:param」タグを「h:outputFormat」に付加する場合、パラメーターはプレースホルダーを指定します。


    
    

出力は次のとおりです–「Hello JSF User. You are from China」。

2. f:param +その他のコンポーネント

h:commandButton」などの他のコンポーネントに「f:param」タグを付けると、パラメータはリクエストパラメータに変わります。


    

ユーザーBeanでは、次のようにパラメーター値を取得できます。

    Map params =
        FacesContext.getExternalContext().getRequestParameterMap();

    String countrry = params.get("country");

JSF f:paramの例

これはJSF2.0アプリケーションで、「h:commandButton」と「h:outputFormat」の両方のコンポーネントでf:paramタグが使用されていることを示しています。

1. マネージドBean

シンプルなマネージドBean。

UserBean.java

package com.example;

import java.util.Map;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;

@ManagedBean(name="user")
@SessionScoped
public class UserBean{

    public String name;
    public String country;

    public String outcome(){

        FacesContext fc = FacesContext.getCurrentInstance();
        this.country = getCountryParam(fc);

        return "result";
    }

    //get value from "f:param"
    public String getCountryParam(FacesContext fc){

        Map params = fc.getExternalContext().getRequestParameterMap();
        return params.get("country");

    }

    //getter and setter methods

}

2. JSFページ

デモンストレーション用の2つのJSFページ。

default.xhtml




    

    

JSF 2 param example

Enter your name :

result.xhtml




    

    

JSF 2 param example

3. Demo

「例」などの名前を入力して、ボタンをクリックします。

jsf2-Param-Example-1

フォーマットされたメッセージ、ユーザー入力からの「名前」、ボタンパラメーターからの「国」を表示します。

jsf2-Param-Example-2

ソースコードをダウンロード

ダウンロード–JSF-2-Param-Example.zip(10KB)