JavaServer Faces (JSF) はJavaベースの Webアプリケーションフレームワーク であり、Java EEアプリケーションのユーザーインターフェイスの開発を簡単にする。表示技術として使用するが、JSFはXULなどの他の表示技術を利用することもできる。

Ajax(エイジャックス、アジャックス)は、ウェブブラウザ内で非同期通信とインターフェイスの構築などを行う技術の総称。XMLHttpRequest(HTTP通信を行うためのJavaScript組み込みクラス)による非同期通信を利用し、通信結果に応じてダイナミックHTMLで動的にページの一部を書き換えるというアプローチを取る。

最近、Web 2.0の発展にしたがって、Ajaxも広くてよく使われている。もともとWeb上で実現できない操作も、Ajaxを利用して素敵な実現できた。下記はJSF中にAjaxを利用するサンプルである。

まずは、下記のようなJSFソース:

  1. import javax.faces.event.ActionEvent;
  2. import javax.faces.model.ManagedBean;
  3. import javax.faces.model.SessionScoped;
  4.  
  5. @ManagedBean(name = "count")
  6. @SessionScoped
  7. public class Count {
  8.     Integer count = 0;
  9.  
  10.     public Integer getCount() {
  11.         return count++;
  12.     }
  13.  
  14.     public void reset(ActionEvent ae) {
  15.         count = 0;
  16.     }
  17. }

下記はフェイス部分のサンプルソース:

  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  2.         "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  3. <html xmlns="http://www.w3.org/1999/xhtml" 
  4.       xmlns:h="http://java.sun.com/jsf/html">
  5. <h:head>
  6.     <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"/>
  7.     <title>Ajax</title>
  8. </h:head>
  9. <h:body>
  10.     <h:form id="form1" prependId="false">
  11.         <h:outputScript name="ajax.js" library="javax.faces" target="head"/>
  12.         <h:outputText id="out1" value="#{count.count}"/>
  13.         <br/>
  14.         <!-- Increment the counter on the server, and the client -->
  15.         <h:commandButton id="button1" value="Count"
  16.                          onclick="javax.faces.Ajax.ajaxRequest(this, event, {execute: this.id, render: 'out1'}); return false;"/>
  17.         <br/>
  18.         <!-- Resets the counter -->
  19.         <h:commandButton id="reset" value="reset"
  20.                             onclick="javax.faces.Ajax.ajaxRequest(this, event, {execute:'reset', render: 'out1'}); return false;"
  21.                             actionListener="#{count.reset}"/>
  22.     </h:form>
  23. </h:body>
  24. </html>
メインコンテンツEND ■
1 Star2 Stars3 Stars4 Stars5 Stars (No Ratings Yet)
Loading ... Loading ...

Posted on Thursday, 6th November 2008 by admin

Tags: , , , , ,
Posted in Ajax/JavaScript, Java | Comments (0) | 2,428 views

Leave a Reply