別のブラウザで表示すると、JavaScriptによってこのドキュメントの表示形式が変わる場合があります。ただしドキュメントの内容に影響はありません。

UIX開発者ガイド Go to Table of Contents
目次
Go to previous page
前へ
Go to next page
次へ

15. Strutsを利用したuiXMLの使用方法

Strutsフレームワークは、現在ではJ2EE Webアプリケーションを構築するための最も一般的なフレームワークの1つです。Strutsの開発者はページにJSPを頻繁に使用しますが、Strutsでは別の表示テクノロジの使用もサポートされています。UIXで提供される一連の拡張機能により、Strutsアプリケーションの一部またはすべてに対してuiXMLを使用できます。また、標準的なStrutsのアクション、ActionForm Beansおよび構成ファイルをすべて使用して、uiXMLベースのアプリケーションを制御できます。

このトピックでは、Struts開発に関する知識があることを前提にしています。必要に応じてStrutsサイトにアクセスし、このテクノロジについて学んでください。

ここでは、次の項目について説明します。

Struts拡張機能のインストール

ここでは、UIXとStrutsの両方がすでにインストールされていることが前提となります(ただし、UIXにはStruts 1.1 beta 1以上が必要です)。次に、UIXのStruts拡張機能を登録する必要があります。最小限のWEB-INF/uix-config.xmlファイルは次のとおりです。

注意:
アプリケーションのweb.xmlファイルに定義されるUIXサーブレットのoracle.cabo.ui.UIExtensions初期化パラメータにoracle.cabo.servlet.struts.StrutsUIExtensionの値がセットされている場合、以下の手順は必要ありません。uiXML Strutsのコンポーネント・パレットを使用してuiXMLページの編集を行った場合は、このような設定は既に行われています。


<?xml version="1.0" encoding="ISO-8859-1"?>
<configurations xmlns="http://xmlns.oracle.com/uix/config">

  <application-configuration>
    <ui-extensions>
      <extension-class>oracle.cabo.servlet.struts.StrutsUIExtension</extension-class>
    </ui-extensions>
  </application-configuration>

</configurations>

この構成ファイルを見るのが初めての場合は、必要に応じて「構成」のトピックを参照してください。

必要なものはこれですべてです。セットアップは終了しているので、基本的な2ページのログイン・アプリケーションの作成を開始します。まず、Strutsコードを記述します。後述するように、これはUIXへの参照のない完全に汎用的なStrutsコードです。コントローラのコードに表示コードへの依存性を持たせるのは望ましくないため、これは良い傾向です。

例: Strutsログイン・コード

ここでは、Struts構成ファイル、ログイン情報を格納するLogonBean、ログインを処理するLogonAction、およびメッセージを格納する小さな .propertiesファイルの4つのファイルを使用します。Strutsの知識があるユーザーにとっては理解が容易なため、このコードの詳細は説明しません。

struts-config.xml


<?xml version="1.0"?>
<!DOCTYPE struts-config SYSTEM "struts-config_1_1.dtd">
<struts-config>
  <form-beans>
    <form-bean
      name="logonForm"
      type="LogonBean" />
  </form-beans>
  <global-forwards
      type="org.apache.struts.action.ActionForward">
    <forward name="welcome" path="/welcome.uix"
         redirect="false" />
  </global-forwards>
  <action-mappings>
    <action
        path="/logon"
        type="LogonAction"
        name="logonForm"
        scope="request"
        input="/logon.uix"
        unknown="false"
        validate="true"/>
  </action-mappings>

  <message-resources parameter="LogonText"/>
</struts-config>

LogonBean:


import javax.servlet.http.HttpServletRequest;
import org.apache.struts.action.ActionError;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;

public class LogonBean extends ActionForm
{
  public String getUser()
  {
    return _user;
  }

  public void setUser(String user)
  {
    _user = user;
  }

  public String getPassword()
  {
    return _password;
  }

  public void setPassword(String password)
  {
    _password = password;
  }

  public ActionErrors validate(
    ActionMapping mapping, HttpServletRequest request)
  {
    // For the purposes of this demo, just call
    // "123" a good password!
    if (!"123".equals(_password))
    {
      ActionErrors errors = new ActionErrors();
      errors.add("pwd", new ActionError("password"));
      return errors;
    }

    return null;
  }

  private String _user;
  private String _password;
}

LogonAction:


import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import org.apache.struts.action.ActionMessage;
import org.apache.struts.action.ActionMessages;

public class LogonAction extends Action
{
  public ActionForward execute(
    ActionMapping mapping,
    ActionForm    form,
    HttpServletRequest request,
    HttpServletResponse response)
  {
    LogonBean bean = (LogonBean) form;
    // Tell the user the login was successful
    ActionMessages messages = new ActionMessages();
    messages.add(ActionMessages.GLOBAL_MESSAGE,
                 new ActionMessage("loggedIn", bean.getUser()));
    saveMessages(request, messages);

    // And go to the generic "welcome" page
    return mapping.findForward("welcome");
  }
}

LogonText.properties:


password=Bogus password! (Try 123)
loggedIn=Hello {0}, you logged in successfully!

前述されているとおり、これはまったく汎用的なStrutsコードです。(たとえば、123というパスワードを入力して)ユーザーがログインに成功した場合、検証が成功し、LogonActionによりユーザーが初期画面ページにリダイレクトされます。初期画面ページは、Struts構成ファイル内で、welcome.uixにより処理されるように定義されています。

uiXMLページの記述

UIXのログイン・ページと初期画面ページをStrutsなしで記述する場合は、次のようになります。

logon.uix:


<page xmlns="http://xmlns.oracle.com/uix/controller"
      xmlns:ctrl="http://xmlns.oracle.com/uix/controller">
 <content>
 <dataScope xmlns="http://xmlns.oracle.com/uix/ui"
            xmlns:data="http://xmlns.oracle.com/uix/ui">
   <contents>
    <pageLayout>
      <contents>
        <form name="theForm" method="post">
          <contents>
            <labeledFieldLayout>
              <contents>
                <messageTextInput prompt="User" name="user"/>
                <messageTextInput secret="true" prompt="Password"
                                  name="password"/>
                <submitButton ctrl:event="login" text="Log On"/>
              </contents>
            </labeledFieldLayout>
          </contents>
        </form>
      </contents>
    </pageLayout>
   </contents>
  </dataScope>
 </content>
 <handlers>
   <event name="login">
     <!-- Here, we handle the event... somehow -->
   </event>
 </handlers>
</page>

welcome.uix:


<page xmlns="http://xmlns.oracle.com/uix/controller"
      xmlns:ctrl="http://xmlns.oracle.com/uix/controller">
 <content>
 <dataScope xmlns="http://xmlns.oracle.com/uix/ui"
            xmlns:data="http://xmlns.oracle.com/uix/ui">
   <contents>
    <pageLayout title="Hi there!"/>
   </contents>
  </dataScope>
 </content>
</page>

この2つのページに関して唯一注意が必要なのは、userとpasswordという2つの<messageTextInput>要素の名前です。これらの名前は、LogonBeanの2つのプロパティの名前に対応しています。このページに連結するとき、Strutsはこの要素に依存します。

Strutsアクションの追加

これらは単純な2つのページですが、まだ何も処理を行いません。まず、LogonActionクラスと関連付けます。

Strutsアクション・ハンドラを使用したlogon.uix:


<page xmlns="http://xmlns.oracle.com/uix/controller"
      xmlns:ctrl="http://xmlns.oracle.com/uix/controller"
      xmlns:struts="http://xmlns.oracle.com/uix/struts">
 <content>
 <dataScope xmlns="http://xmlns.oracle.com/uix/ui"
            xmlns:data="http://xmlns.oracle.com/uix/ui">
   <contents>
    <pageLayout>
      <contents>
        <form name="theForm" method="post">
          <contents>
            <labeledFieldLayout>
              <contents>
                <messageTextInput prompt="User" name="user"/>
                <messageTextInput secret="true" prompt="Password"
                                  name="password"/>
                <submitButton ctrl:event="login" text="Log On"/>
              </contents>
            </labeledFieldLayout>
          </contents>
        </struts:form>
      </contents>
    </pageLayout>
   </contents>
  </dataScope>
 </content>
 <handlers>
   <event name="login">
     <struts:action path="/logon.do"/>
   </event>
 </handlers>
</page>

このページに2箇所コードを追加しました。最初に、XMLネームスペースhttp://xmlns.oracle.com/uix/strutsをstruts接頭辞に割り当てました。次に、loginイベントを取り扱うための<struts:action>イベントを使用しました。

これで、機能する2つのページが完成しました。間違ったパスワードを入力すると、ログインを再試行するように求められます。正しいパスワードを入力すると、初期画面ページが表示されます。しかし、まだ修正の必要な点が3つあります。

どちらも、もう一方のUIX Struts XML要素を使用すれば簡単に解決できます。

<struts:form>の使用

ユーザーが入力した値を保存するため、標準的なuiXML <form>要素を特殊なuiXML <struts:form>要素に変更します。また、<messageTextInput>要素を<struts:messageTextInput>に変更します。

<struts:form>を使用したlogon.uix:


<page xmlns="http://xmlns.oracle.com/uix/controller"
      xmlns:ctrl="http://xmlns.oracle.com/uix/controller"
      xmlns:struts="http://xmlns.oracle.com/uix/struts">
 <content>
 <dataScope xmlns="http://xmlns.oracle.com/uix/ui"
            xmlns:data="http://xmlns.oracle.com/uix/ui">
   <contents>
    <pageLayout>
      <contents>
        <struts:form name="theForm" method="post"
                      beanName="logonForm">
          <contents>
            <labeledFieldLayout>
              <contents>
                <struts:messageTextInput prompt="User" name="user"/>
                <struts:messageTextInput secret="true" prompt="Password"
                                  name="password"/>
                <submitButton ctrl:event="login" text="Log On"/>
              </contents>
            </labeledFieldLayout>
          </contents>
        </struts:form>
      </contents>
    </pageLayout>
   </contents>
  </dataScope>
 </content>
 <handlers>
   <event name="login">
     <struts:action path="/do/logon"/>
   </event>
 </handlers>
</page>

<form>および<messageTextInput>をStrutsバージョンに変更した以外に、<struts:form>にbeanName属性を設定しました。この属性は必須であり、struts-config.xml内に定義されている<form-bean>名(typeではなくname)と一致している必要があります。

これを試行すると、パスワードのエラーが発生した場合、userについてはユーザーが入力した内容が自動的に復元されることがわかります。しかし、passwordについては復元されません。これは意図的なものです。パスワードは、ブラウザでは連続するアスタリスクのように見えても、HTMLではプレーン・テキストで送信されます。

<struts;form>および<struts:messageTextInput>の他にも、uiXMLには次の要素のStruts要素が含まれます。

Strutsメッセージの表示

「エラーの処理」のトピックをすでに参照しているユーザーは、<messageBox>要素とインライン・メッセージについて理解しているはずです。UIXには特殊なStruts <struts:messageBox>要素は含まれてなく、またその必要もありません。かわりに、特殊な<struts:dataScope>要素を利用できます。

<struts:dataScope>要素には標準の<dataScope>のすべての機能が備わっているため、すでにページで<dataScope>要素を使用している場合は、Strutsバージョンで置き換えてください。通常のdata-scope機能のほかにStrutsバージョンでは、自動的にActionMessageオブジェクトまたはActionErrorオブジェクトが検出され、UIXメッセージのリストに追加されます。

Strutsメッセージを使用したlogon.uix:


<page xmlns="http://xmlns.oracle.com/uix/controller"
      xmlns:ctrl="http://xmlns.oracle.com/uix/controller"
      xmlns:struts="http://xmlns.oracle.com/uix/struts">
 <content>
 <struts:dataScope xmlns="http://xmlns.oracle.com/uix/ui"
            xmlns:data="http://xmlns.oracle.com/uix/ui">
   <contents>
    <pageLayout>
      <messages>
        <messageBox automatic="true"/>
      </messages>

      <contents>
        <struts:form name="theForm" method="post"
                      beanName="logonForm">
          <contents>
            <labeledFieldLayout>
              <contents>
                <struts:messageTextInput prompt="User" name="user"/>
                <struts:messageTextInput secret="true" prompt="Password"
                                  name="password">
                  <boundMessage select="pwd"/>
                </struts:messageTextInput>
                <submitButton ctrl:event="login" text="Log On"/>
              </contents>
            </labeledFieldLayout>
          </contents>
        </struts:form>
      </contents>
    </pageLayout>
   </contents>
  </struts:dataScope>
 </content>
 <handlers>
   <event name="login">
     <struts:action path="/do/logon"/>
   </event>
 </handlers>
</page>

Strutsメッセージを使用したwelcome.uix:


<page xmlns="http://xmlns.oracle.com/uix/controller"
      xmlns:ctrl="http://xmlns.oracle.com/uix/controller"
      xmlns:struts="http://xmlns.oracle.com/uix/struts">
 <content>
 <struts:dataScope xmlns="http://xmlns.oracle.com/uix/ui"
            xmlns:data="http://xmlns.oracle.com/uix/ui">
   <contents>
    <pageLayout title="Hi there!">
      <messages>
        <messageBox automatic="true"/>
      </messages>
    </pageLayout>
   </contents>
  </struts:dataScope>
 </content>
</page>

<dataScope>を<struts:dataScope>に変更し、標準の<messageBox>を追加しました。また、ログイン・ページで、パスワード・フィールドの内部に<boundMessage>要素を追加しました。これにより、インライン・エラー・メッセージが自動的に追加されます。ここでのpwdという文字列は、LogonBeanvalidate()メソッドで使用されている文字列と一致します。