SHOEISHA iD

※旧SEメンバーシップ会員の方は、同じ登録情報(メールアドレス&パスワード)でログインいただけます

DeveloperZine(デベロッパージン)- エンジニアの意思決定を支える技術情報メディア ProductZine

CodeZine編集部では、現場で活躍するデベロッパーをスターにするためのカンファレンス「Developers Summit」や、エンジニアの生きざまをブーストするためのイベント「Developers Boost」など、さまざまなカンファレンスを企画・運営しています。

japan.internet.com翻訳記事

Ajax4JSFを利用したデータ検証

フレームワークを活用したAjaxの導入

バッキングBeanクラス

 バッキングBeanクラス「Input.java」のコードを、リスト3に示します。

リスト3 Input.java
package ajaxjsf.backing;

import java.sql.*;

import javax.faces.component.html.HtmlCommandButton;
import javax.faces.component.html.HtmlForm;
import javax.faces.component.html.HtmlInputText;
import javax.faces.component.html.HtmlOutputLabel;
import javax.faces.component.html.HtmlOutputText;
import javax.faces.component.html.HtmlPanelGrid;

import javax.naming.InitialContext;

public class Input {
  private HtmlForm form1;
  private HtmlPanelGrid panelGrid1;
  private HtmlOutputLabel outputLabel1;
  private HtmlInputText inputText1;
  private HtmlOutputLabel outputLabel2;
  private HtmlInputText inputText2;
  private HtmlOutputLabel outputLabel3;
  private HtmlInputText inputText3;
  private HtmlOutputLabel outputLabel4;
  private HtmlInputText inputText4;
  private HtmlOutputLabel outputLabel5;
  private HtmlInputText inputText5;
  private HtmlOutputLabel outputLabel6;
  private HtmlInputText inputText6;
  private HtmlCommandButton commandButton1;
  private HtmlOutputText outputText1;

  public void setForm1(HtmlForm form1) {
    this.form1 = form1;
  }

  public HtmlForm getForm1() {
    return form1;
  }

  public void setPanelGrid1(HtmlPanelGrid panelGrid1) {
    this.panelGrid1 = panelGrid1;
  }

  public HtmlPanelGrid getPanelGrid1() {
    return panelGrid1;
  }

  public void setOutputLabel1(HtmlOutputLabel outputLabel1) {
    this.outputLabel1 = outputLabel1;
  }

  public HtmlOutputLabel getOutputLabel1() {
    return outputLabel1;
  }

  public void setInputText1(HtmlInputText inputText1) {
    this.inputText1 = inputText1;
  }

  public HtmlInputText getInputText1() {
    return inputText1;
  }

  public void setOutputLabel2(HtmlOutputLabel outputLabel2) {
    this.outputLabel2 = outputLabel2;
  }

  public HtmlOutputLabel getOutputLabel2() {
    return outputLabel2;
  }

  public void setInputText2(HtmlInputText inputText2) {
    this.inputText2 = inputText2;
  }

  public HtmlInputText getInputText2() {
    return inputText2;
  }

  public void setOutputLabel3(HtmlOutputLabel outputLabel3) {
    this.outputLabel3 = outputLabel3;
  }

  public HtmlOutputLabel getOutputLabel3() {
    return outputLabel3;
  }

  public void setInputText3(HtmlInputText inputText3) {
    this.inputText3 = inputText3;
  }

  public HtmlInputText getInputText3() {
    return inputText3;
  }

  public void setOutputLabel4(HtmlOutputLabel outputLabel4) {
    this.outputLabel4 = outputLabel4;
  }

  public HtmlOutputLabel getOutputLabel4() {
    return outputLabel4;
  }

  public void setInputText4(HtmlInputText inputText4) {
    this.inputText4 = inputText4;
  }

  public HtmlInputText getInputText4() {
    return inputText4;
  }

  public void setOutputLabel5(HtmlOutputLabel outputLabel5) {
    this.outputLabel5 = outputLabel5;
  }

  public HtmlOutputLabel getOutputLabel5() {
    return outputLabel5;
  }

  public void setInputText5(HtmlInputText inputText5) {
    this.inputText5 = inputText5;
  }

  public HtmlInputText getInputText5() {
    return inputText5;
  }

  public void setOutputLabel6(HtmlOutputLabel outputLabel6) {
    this.outputLabel6 = outputLabel6;
  }

  public HtmlOutputLabel getOutputLabel6() {
    return outputLabel6;
  }

  public void setInputText6(HtmlInputText inputText6) {
    this.inputText6 = inputText6;
  }

  public HtmlInputText getInputText6() {
    return inputText6;
  }

  public void setCommandButton1(
    HtmlCommandButton commandButton1) {
    this.commandButton1 = commandButton1;
  }

  public HtmlCommandButton getCommandButton1() {
    return commandButton1;
  }

  public void setOutputText1(HtmlOutputText outputText1) {
    this.outputText1 = outputText1;
  }

  public HtmlOutputText getOutputText1() {
    return outputText1;
  }

  public String inputText_action() {
    ResultSet rs = null;
      try {
        InitialContext initialContext = new InitialContext();
        javax.sql.DataSource ds = 
        (javax.sql.DataSource)initialContext.lookup(
        "java:comp/env/jdbc/OracleDBConnectionDS");
        java.sql.Connection connection = ds.getConnection();

          Statement stmt = connection.createStatement(
            ResultSet.TYPE_SCROLL_INSENSITIVE, 
            ResultSet.CONCUR_READ_ONLY);

            String catalogID = (String)inputText1.getValue();

            String query = "SELECT * from Catalog WHERE 
              CATALOGID=" + "'" + catalogID + "'";
            rs = stmt.executeQuery(query);
            if (rs.next()) {
              inputText2.setValue(rs.getString(2));
              inputText3.setValue(rs.getString(3));
              inputText4.setValue(rs.getString(4));
              inputText5.setValue(rs.getString(5));
              inputText6.setValue(rs.getString(6));
              outputText1.setValue(new String(
                "Catalog Id is not Valid"));

              commandButton1.setDisabled(true);

            } else {
              inputText2.setValue(new String());
              inputText3.setValue(new String());
              inputText4.setValue(new String());
              inputText5.setValue(new String());
              inputText6.setValue(new String());
              outputText1.setValue(new String(
                "Catalog Id is Valid"));

              commandButton1.setDisabled(false);
            }

      } catch (SQLException e) {
        System.out.println(e.getMessage());
      } catch (javax.naming.NamingException e) {
        System.out.println(e.getMessage());
    }
    return null;
  }

  public String commandButton_action() {
    try {
      //Obtain Connection
      InitialContext initialContext = new InitialContext();
      javax.sql.DataSource ds = 
        (javax.sql.DataSource)initialContext.lookup(
        "java:comp/env/jdbc/OracleDBConnectionDS");
        java.sql.Connection conn = ds.getConnection();

        String catalogId = (String)inputText1.getValue();
        String journal = (String)inputText2.getValue();
        String publisher = (String)inputText3.getValue();
        String edition = (String)inputText4.getValue();
        String title = (String)inputText5.getValue();
        String author = (String)inputText6.getValue();

        Statement stmt = conn.createStatement();
        String sql = 
          "INSERT INTO Catalog VALUES(
          " + "\'" + catalogId + "\'" + "," + "\'" + journal + 
          "\'" + "," + "\'" + publisher + "\'" + "," + "\'" + 
          edition + "\'" + "," + "\'" + title + "\'" + "," + 
          "\'" + author + "\'" + ")";

          stmt.execute(sql);

          stmt.close();
          conn.close();

    } catch (javax.naming.NamingException e) {
      return "error";
    } catch (SQLException e) {
      return "error";
    }

      return "catalog";

    }
}

応答の処理

 アプリケーションを実行して、フォームに指定されたID値を、Catalogデータベーステーブル内のデータに対して検証することができます。入力されたID値が既存の値の場合、アプリケーションはエントリを無効と判断し、その結果を伝えるメッセージを表示します。既存レコードのその他のフォームフィールド値も表示され、[Submit]ボタンが無効になります。

 入力されたID値がデータベーステーブルに存在しない場合、アプリケーションはエントリを有効と判断し、その結果を伝えるメッセージを表示します。他のフォームフィールドは空のString値に設定され、[Submit]ボタンが有効になります。

 有効なIDの場合は、カタログエントリを作成し、[Submit]ボタンを使ってそのエントリを格納できます。<a4j:support>タグのreRender属性で設定されるJSFコンポーネントは、AJAX応答で更新されるコンポーネントを指定します。

 「input.jsp」を右クリックし、[Run]を選択して、JSFページを実行します。Catalog IDフィールドに値を指定します。フィールドが変更されるたびに(文字が入力されるたびに)AJAX要求が送信され、ID番号が一意(有効)であることを示す検証メッセージが表示されます。

 次に、データベースに既に存在するID値、例えば「catalog2」を指定してみましょう。「Catalog Id is not valid」メッセージが表示されることに注目してください。値がデータベースに存在するため、オートコンプリート機能によってフォームフィールドに値が設定され、[Submit]ボタンが無効になります。

 これまでの説明でお分かりのように、Ajax4JSFコンポーネントライブラリによってJSFアプリケーションにAJAX機能を追加できます。AJAX機能を追加するためにJavaScriptコードをわざわざ作成する必要はありません。

この記事は参考になりましたか?

連載通知を行うには会員登録(無料)が必要です。
既に会員の方はを行ってください。
japan.internet.com翻訳記事連載記事一覧

もっと読む

この記事の著者

japan.internet.com(ジャパンインターネットコム)

japan.internet.com は、1999年9月にオープンした、日本初のネットビジネス専門ニュースサイト。月間2億以上のページビューを誇る米国 Jupitermedia Corporation (Nasdaq: JUPM) のニュースサイト internet.comEarthWeb.com からの最新記事を日本語に翻訳して掲載するとともに、日本独自のネットビジネス関連記事やレポートを配信。

※プロフィールは、執筆時点、または直近の記事の寄稿時点での内容です

Deepak Vohra(Deepak Vohra)

O'Reillyの技術レビュー担当者として『WebLogic: The Definitive Guide』を担当。NuBeanコンサルタント、Web開発者でもある。Sun認定資格(Java 1.4プログラマおよびWebコンポーネントディベロッパJ2EEバージョン)を取得。メールの宛先はdvohra0...

※プロフィールは、執筆時点、または直近の記事の寄稿時点での内容です

この記事は参考になりましたか?

この記事をシェア

CodeZine(コードジン)
https://codezine.jp/article/detail/1616 2007/08/24 14:00

イベント

CodeZine編集部では、現場で活躍するデベロッパーをスターにするためのカンファレンス「Developers Summit」や、エンジニアの生きざまをブーストするためのイベント「Developers Boost」など、さまざまなカンファレンスを企画・運営しています。

新規会員登録無料のご案内

  • ・全ての過去記事が閲覧できます
  • ・会員限定メルマガを受信できます

メールバックナンバー