Webアプリにおけるサーバとクライアントの常識Webアプリの常識をJSPとStrutsで身につける(3)(3/3 ページ)

» 2008年09月01日 00時00分 公開
[鬼木実株式会社メセナ・ネットコム]
前のページへ 1|2|3       

サンプルにおけるクライアントとサーバの関係性

 クライアントとサーバの関係を詳しく説明したところで、前回紹介したサンプルアプリケーションのクライアントとサーバの関係性を見ていくとともにStrutsの構成要素を説明します。

図5 サンプルアプリケーションの構成図 図5 サンプルアプリケーションの構成図

 サンプルアプリケーションでは、まずクライアント側から前回のサンプルアプリケーションのWho.jspがWebブラウザ上から表示されている状態とします。

図6 質問画面(再掲) 図6 質問画面(再掲)

 名前をテキストボックスへ入力して、「OK」ボタンを押すと、サーバ側へ入力したテキストボックスの情報がリクエストとしてサーバ側へ渡ります。サーバ側では、Strutsが提供するActionServletクラスがリクエストの情報をすべて受け取ります。

Who.jsp(再掲)
<%@ page contentType="text/html; charset=Shift-JIS" %>
<%@ taglib uri="http://struts.apache.org/tags-html" 
  prefix="html" %>
<html:html>
  <head>
    <title>Who</title>
  </head>
  <html:form action="/hello">
    <table border="0">
      <html:errors/>
      <tr><td>
        あなたの名前は?<br>
        <html:text property="name" size="20" maxlength="30" />です。
      </td></tr>
      <tr><td>
        <html:submit value="OK" />
      </td></tr>
    </table>
  </html:form>
</html:html>

 リクエストを受け取った後、Strutsはリクエストパス(struts-config.xmlで設定した情報)と一致したActionFormクラス(HelloFormクラスに継承)とActionクラス(HelloActionクラスに継承)を取得し、ActionFormクラスを引数にしてActionクラスのexecute()メソッドを呼び出します。

struts-config.xml(再掲)
<?xml version="1.0" encoding="Shift-JIS" ?>
<!DOCTYPE struts-config PUBLIC
  "-//Apache Software Foundation//DTD Struts Configuration 1.3//EN"
  "http://struts.apache.org/dtds/struts-config_1_3.dtd">
<struts-config>
  <!-- ========================= Form Bean Definitions -->
  <form-beans>
    <form-bean name="HelloForm" type="hello.form.HelloForm" />
  </form-beans>
  <!-- ========================= Global Exception Definitions -->
  <global-exceptions>
  </global-exceptions>
  <!-- ========================= Global Forward Definitions -->
  <global-forwards>
  </global-forwards>
  <!-- ========================= Action Mapping Definitions -->
  <action-mappings>
    <action
      attribute="HelloForm"
      input="/pages/Who.jsp"
      name="HelloForm"
      path="/hello"
      type="hello.HelloAction"
      scope="session"
      validate="true">
        <forward name="success" path="/pages/Hello.jsp" />
    </action>
  </action-mappings>
  <!-- ========================== Message Resources Definitions -->
  <!-- ========================== Plug Ins Configuration -->
  <!-- ========================== Tiles plugin -->
  <!-- ========================== Validator plugin -->
</struts-config>

HelloForm.java(再掲)
package hello.form;

import org.apache.struts.action.ActionForm;

/**
* HelloForm.java
*/

public class HelloForm extends ActionForm {

    private String name;

    /**
    *
    * @return
    */

    public String getName() {
        return name;
    }
    /**
    *
    * @param name
    */

    public void setName(String name) {
        this.name = name;
    }
}

 次に、呼び出したActionクラスのexecute()メソッドの戻り値として受け取ったActionForwardクラスを基に、遷移先のJSPファイル(Hello.jsp)にフォワードします。

HelloAction.java(再掲)
package hello;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import hello.form.HelloForm;

/**
* HelloAction.java
*/

public class HelloAction extends Action {

    Log log = LogFactory.getLog(HelloAction.class);

    public ActionForward execute(ActionMapping mapping,
                                    ActionForm form,
                                    HttpServletRequest request,
                                    HttpServletResponse response)
    throws Exception {

        request.setCharacterEncoding("Windows-31J");
        HelloForm helloForm = (HelloForm) form;
        log.info(" user = "+helloForm.getName());

        return mapping.findForward("success");
    }
}

 JSPファイルにはStrutsのタグライブラリ(下記「<%@ taglib 」「<html:」「<bean:」以下の赤太字を参照)が利用されており、ActionFormクラスの情報を表示します。

Hello.jsp(再掲)
<%@ page contentType="text/html; charset=Shift-JIS" %>
<%@ taglib uri="http://struts.apache.org/tags-html" 
  prefix="html" %>
<%@ taglib uri="http://struts.apache.org/tags-bean" 
  prefix="bean" %>
<html:html>
  <head>
    <title>Hello</title>
  </head>
  <html:form action="/hello">
    <table border="0">
      <tr><td>
        ようこそ<bean:write name="HelloForm" property="name" />さん!
      </td></tr>
    </table>
  </html:form>
</html:html>

 そして、レスポンスとしてクライアント側のWebブラウザ上へ表示するといった流れになっています。

図7 回答画面(再掲) 図7 回答画面(再掲)

 ここで、各ファイルの説明を簡単ですが一覧で紹介します。

表3 サンプルの各ファイル
ファイル 説明 作成する
必要の有無
ActionServlet.java Webブラウザから送信されたリクエストを受け取り、どの処理を行うか記載されてる ×
ActionForm.java Webブラウザから送信されたデータを保持
Action.java Webブラウザからリクエストされた処理を実行
ActionForward.java Actionクラスに記述された処理を行った後の次画面を指定している ×
Strutsライブラリ JSPで利用するStruts専用タグライブラリ ×
struts-config.xml Strutsアプリケーションにおけるファイル間の対応付けを定義している。XML形式の設定ファイル

C/Sを理解すれば、Webアプリはそれほど難しくない

 今回の内容で、クライアントとサーバの関係を詳しく理解できたでしょうか。前回の記事で紹介したとおり、Webアプリケーションを作成することはそれほど難しいわけではありません。今回紹介する概観を知っておくことでStrutsやサーブレット/JSP、ひいてはWebアプリケーション自体への深い理解が得られたかと思います。

 次回からは、今回少しだけ触れたJSPで利用するStruts専用のタグライブラリなどについて説明していきます。

プロフィール

鬼木 実(おにき まこと)
株式会社メセナ・ネットコム所属

現在は主に現行システムの調査・分析を行っている。何を求めているのかを考え、柔軟な思考と創造力を持った「真の技術者」を目指して成長し続けている。
趣味は作曲、読書、チェス。



前のページへ 1|2|3       

Copyright © ITmedia, Inc. All Rights Reserved.

RSSについて

アイティメディアIDについて

メールマガジン登録

@ITのメールマガジンは、 もちろん、すべて無料です。ぜひメールマガジンをご購読ください。