Jsf-expression-language

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

JSF-式言語

JSFは豊富な表現言語を提供します。 *#\ {operation-expression} *表記を使用して通常の操作を記述できます。 JSF式言語の利点の一部を次に示します。

  • Beanプロパティを参照できます。Beanは、リクエスト、セッション、またはアプリケーションスコープに格納されているオブジェクト、または管理対象Beanです。
  • リスト、マップ、配列などのコレクションの要素に簡単にアクセスできます。
  • リクエストなどの定義済みオブジェクトに簡単にアクセスできます。
  • 式言語を使用して、算術演算、論理演算、およびリレーショナル演算を実行できます。
  • 自動型変換。
  • NullPointerExceptionの代わりに空の文字列として欠損値を表示します。

応用例

式言語をテストするテスト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 UserData.java under package com.finddevguides.test as explained below.
3 Modify home.xhtml as explained below. Keep the rest of the files unchanged.
4 Compile and run the application to make sure the business logic is working as per the requirements.
5 Finally, build the application in the form of war file and deploy it in Apache Tomcat Webserver.
6 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.Date;

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;
   private Date createTime = new Date();
   private String message = "Hello World!";

   public Date getCreateTime() {
      return(createTime);
   }

   public String getMessage() {
      return(message);
   }
}

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: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>Expression Language Example</h2>
      Creation time:
      <h:outputText value = "#{userData.createTime}"/>
      <br/><br/>
      Message:
      <h:outputText value = "#{userData.message}"/>
   </h:body>
</html>

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

JSF式言語の結果