LoginSignup
0
0

Javaプログラミング入門:ステップ3 フレームワークの習得

Posted at

Javaプログラミング入門:ステップ3 フレームワークの習得

JavaのWebアプリケーション開発において、フレームワークを利用することで効率的に開発を進めることができます。Strutsは、Java EE(Enterprise Edition)プラットフォーム向けの代表的なフレームワークの一つです。本記事では、Strutsの基礎を学ぶためのステップ3として、以下の内容を順を追って解説します。

  1. Strutsの基本概念
  2. Strutsの設定(struts-config.xmlなど)
  3. アクションフォームとアクションクラスの作成
  4. JSPとの連携

1. Strutsの基本概念

Strutsは、Apache Software Foundationが開発したMVC(Model-View-Controller)アーキテクチャに基づくWebアプリケーションフレームワークです。Strutsは、以下の主要コンポーネントで構成されています。

  • モデル(Model): ビジネスロジックやデータアクセスを担当します。
  • ビュー(View): ユーザーに表示されるUIを担当します。通常、JSPを使用します。
  • コントローラ(Controller): ユーザーからのリクエストを処理し、適切なレスポンスを生成します。Strutsでは、ActionServletがコントローラとして機能します。

2. Strutsの設定(struts-config.xmlなど)

Strutsアプリケーションを設定するために、struts-config.xmlファイルを使用します。このファイルには、アクションマッピング、フォームビーンの定義、フォワードの設定などが含まれます。

struts-config.xmlの基本構造

struts-config.xmlファイルには、以下の要素が含まれます。

  • form-beans: フォームビーンの定義を行います。
  • action-mappings: アクションマッピングの定義を行います。各アクションに対して、どのアクションクラスが対応するかを指定します。
  • forwards: アクションの実行結果に応じて、どのJSPページにフォワードするかを指定します。
<!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.2//EN"
    "http://jakarta.apache.org/struts/dtds/struts-config_1_2.dtd">
<struts-config>
    <form-beans>
        <form-bean name="myForm" type="com.example.MyForm" />
    </form-beans>
    
    <action-mappings>
        <action path="/myAction" type="com.example.MyAction" name="myForm" scope="request" validate="true" input="/myForm.jsp">
            <forward name="success" path="/success.jsp" />
            <forward name="failure" path="/failure.jsp" />
        </action>
    </action-mappings>
</struts-config>

3. アクションフォームとアクションクラスの作成

アクションフォームの作成

アクションフォームは、ユーザーからの入力データを保持するためのJavaBeanです。アクションフォームは、ユーザーの入力を検証し、サーバーサイドのロジックにデータを渡す役割を果たします。通常、ActionFormクラスを継承して作成されます。

import org.apache.struts.action.ActionForm;

public class MyForm extends ActionForm {
    private String name;
    private int age;

    // ゲッターとセッター
    public String getName() {
        return name;
    }

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

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }
}

アクションクラスの作成

アクションクラスは、ユーザーからのリクエストを処理するためのクラスです。アクションクラスは、Actionクラスを継承して作成され、executeメソッドをオーバーライドしてビジネスロジックを実装します。アクションクラスは、リクエストのパラメータを取得し、ビジネスロジックを実行し、その結果に応じて適切なビューにフォワードします。

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

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

public class MyAction extends Action {
    public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) {
        MyForm myForm = (MyForm) form;
        String name = myForm.getName();
        int age = myForm.getAge();

        // ビジネスロジックを実行
        if (name != null && !name.isEmpty() && age > 0) {
            return mapping.findForward("success");
        } else {
            return mapping.findForward("failure");
        }
    }
}

4. JSPとの連携

Strutsでは、JSPをビューとして使用します。JSPは、フォームデータを表示し、ユーザーからの入力を受け取るためのページです。

JSPの例

<%@ taglib uri="http://struts.apache.org/tags-html" prefix="html" %>
<%@ taglib uri="http://struts.apache.org/tags-bean" prefix="bean" %>
<!DOCTYPE html>
<html>
<head>
    <title>Struts Example</title>
</head>
<body>
    <html:form action="/myAction">
        <table>
            <tr>
                <td>Name:</td>
                <td><html:text property="name" /></td>
            </tr>
            <tr>
                <td>Age:</td>
                <td><html:text property="age" /></td>
            </tr>
            <tr>
                <td colspan="2"><html:submit value="Submit" /></td>
            </tr>
        </table>
    </html:form>
</body>
</html>

JSPファイルでは、フォームを作成し、ユーザーからの入力を受け取ります。Strutsのタグライブラリを使用して、フォームや入力フィールドを簡単に作成することができます。

成功時のJSP

<!DOCTYPE html>
<html>
<head>
    <title>Success</title>
</head>
<body>
    <h1>Form submitted successfully!</h1>
</body>
</html>

フォームの送信が成功した場合に表示されるページです。ユーザーに成功メッセージを表示します。

失敗時のJSP

<!DOCTYPE html>
<html>
<head>
    <title>Failure</title>
</head>
<body>
    <h1>Form submission failed. Please try again.</h1>
</body>
</html>

フォームの送信が失敗した場合に表示されるページです。ユーザーにエラーメッセージを表示します。

以上が、Strutsの基本概念と設定、アクションフォームとアクションクラスの作成、およびJSPとの連携についての解説です。これらの基礎を理解し、実際にコードを試してみることで、Strutsフレームワークの使用方法を習得しましょう。次回は、より高度なフレームワークの機能に挑戦してみましょう。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0