Jsf-param-tag

提供:Dev Guides
移動先:案内検索

JSF-f:param

f:paramタグは、コンポーネントにパラメーターを渡すオプション、または要求パラメーターを渡すオプションを提供します。

JSFタグ

UIコンポーネントにパラメーターを渡す

<h:outputFormat value = "Hello {0}!.">
   <f:param value = "World"/>
</h:outputFormat>

リクエストパラメータを渡す

<h:commandButton id = "submit"
   value = "Show Message" action = "#{userData.showResult}">
   <f:param name = "username" value = "JSF 2.0 User"/>
</h:commandButton>

タグ属性

S.No Attribute & Description
1

id

コンポーネントの識別子

2

binding

バッキングBeanで使用できるコンポーネントへの参照

3

name

このパラメーターコンポーネントのオプションの名前

4

value

このコンポーネントに保存されている値

応用例

上記のタグをテストするテスト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 home.xhtml as explained below. Keep rest of the files unchanged.
3 Create result.xhtml in the webapps directory as explained below.
4 Create UserData.java as a managed bean under package com.finddevguides.test as explained below.
5 Compile and run the application to make sure business logic is working as per the requirements.
6 Finally, build the application in the form of war file and deploy it in Apache Tomcat Webserver.
7 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;

@ManagedBean(name = "userData", eager = true)
@SessionScoped
public class UserData implements Serializable {
   private static final long serialVersionUID = 1L;
   public String data = "1";

   public String getData() {
      return data;
   }

   public void setData(String data) {
      this.data = data;
   }

   public String showResult() {
      FacesContext fc = FacesContext.getCurrentInstance();
      Map<String,String> params =
         fc.getExternalContext().getRequestParameterMap();
      data =  params.get("username");
      return "result";
   }
}

home.xhtml

<!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">
   <head>
      <title>JSF Tutorial!</title>
   </head>

   <body>
      <h2>f:param example</h2>
      <hr/>

      <h:form>
         <h:outputFormat value = "Hello {0}!.">
            <f:param value = "World"/>
         </h:outputFormat>
         <br/>

         <h:commandButton id = "submit"
            value = "Show Message" action = "#{userData.showResult}">
            <f:param name = "username" value = "JSF 2.0 User"/>
         </h:commandButton>
      </h:form>

   </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"
   xmlns:ui = "http://java.sun.com/jsf/facelets">

   <head>
      <title>JSF Tutorial!</title>
   </head>

   <h:body>
      <h2>Result</h2>
      <hr/>
      #{userData.data}
   </h:body>
</html>

すべての変更を完了したら、JSF-最初のアプリケーションの章で行ったようにアプリケーションをコンパイルして実行します。 すべてがアプリケーションで問題ない場合、次の結果が生成されます。

JSF h:param

[メッセージを表示]ボタンを押すと、次の結果が表示されます。

JSF h:param1