591-5●MVCアーキテクチャ(スケジュールリスト)

591-5●MVCアーキテクチャ(スケジュールリスト)


スケジュールリスト


カレンダー付のスケジュールリストを表示するWebアプリケーションを作成します。
まずは以下の仕様を実現するカレンダーJavaアプリケーションを作成し、その処理ロジックをBean化してブラウザでカレンダーを表示させるようにします。そのあとで登録されたスケジュールを表示できるよう拡張します。

【仕様】
西暦と月を入力することにより対象年月のカレンダーを出力させます。さらに、その月の祝祭日名と共に登録されたスケジュールを一覧表示させます。
カレンダー作成Beanとその機能を引き継いだスケジュール表示Beanを作成します(継承モデルを採用)。
BeanのテストはMVパターンで、最終的なプログラムの構成はMVCパターンで実現します。

※エラー処理は、とりあえず抜きにしましょう

ドキュメントルート(プロジェクト名)はscheduleとしサーブレットクラス(urlパターン)はCalenScheとします。
よって接続urlはhttp://localhost:8080/schedule/CalenScheとなります。


※動的Webプロジェクトでscheduleプロジェクトを作成して実装してください。


【プロジェクトの作成】
新たにプロジェクトを作成します。プロジェクト名はscheduleとします。

ファイルメニュー→新規→動的Webプロジェクト→プロジェクト名:schedule
→ターゲットランタイム:Tomcat(Java17)→動的Webモジュールバージョン:4.0→完了

※プロジェクトをTomcatサーバに追加するのを忘れないように!


(1)カレンダー表示Javaプログラムを作成する
カレンダーを出力するJavaプログラムを提示しますのでとりあえずコピーして、実行できるか確認しましょう。
Calendar.java
package jp.ict.aso;
public class Calendar{
int year, month;
int week,end;
public Calendar(int year,int month){
  this.year=year;
  this.month=month;
  this.week = week_of_day(year, month, 1);       /* 1日の曜日を求める */
  this.end = month_last_day(year, month);       /* 月の最終日を求める */
}
public int getYear() {
    return year;
}
public void setYear(int year) {
    this.year = year;
}
public int getMonth() {
    return month;
}
public void setMonth(int month) {
    this.month = month;
}
public int getWeek() {
    return week;
}
public void setWeek(int week) {
    this.week = week;
}
public int getEnd() {
    return end;
}
public void setEnd(int end) {
    this.end = end;
}
/*** 曜日を求める ***/
/* (仮引数)y:西暦年 m:月 d:日 (返却値)曜日 0=日 1=月 2=火 3=水 4=木 5=金 6=土 */
static int week_of_day(int y, int m, int d){
    if(m == 1) {
        y = y - 1;
        m = 13;
    }
    else if(m == 2) {
        y = y - 1;
        m = 14;
    }
  // ツェラーの公式より
    return (5 * y / 4 - y / 100 + y / 400 + (26 * m + 16) / 10 + d ) % 7;
}
/*** 月の最終日 ***/
/* (仮引数)y:西暦年 m:月 (返却値)月の最終日 */
static int month_last_day(int y, int m){
    int d = 0, last[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
    if (m == 2) {
        d = last[m-1] + is_leap_year(y);    /* 閏年なら+1 */
    }
    else if (m >= 1 && m <= 12) {
        d = last[m-1];
    }
    return d;
}
/*** 閏年の判定 ***/
/*(仮引数)y:西暦年 (返却値)1:閏年 0:閏年以外 */
static int is_leap_year(int y){
    int rc = 0;
    if (y % 4 == 0  &&  (y % 100 != 0 || y % 400 == 0)) {
        rc = 1;
    }
    return rc;
}
public static void main(String[] args){
    Calendar c = new Calendar(2021,1);
    int year=c.getYear();
    int month=c.getMonth();
    int week=c.getWeek();
    int end=c.getEnd();
    System.out.printf("  ** %d年%d月 **\n", year, month);
    System.out.printf(" 日 月 火 水 木 金 土\n");
    System.out.printf("---------------------\n");
    for (int i = 1; i <= week; i++) {      /* 1日まで空白で埋める */
        System.out.print("   ");
    }
    for (int j = 1; j <= end; j++) {      /* 最終日まで表示する */
        System.out.printf(" %2d", j);
        if ( (j + week) % 7 == 0) {    /* 土曜日で改行する */
            System.out.println("");
        }
    }
    System.out.printf("\n");
}
}

【実行結果】
  ** 2021年1月 **
日 月 火 水 木 金 土
---------------------
                1  2
 3  4  5  6  7  8  9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 30
31

(2)カレンダー部品(Bean)を作成する
Calendar.javaからCalendarHtmlBean.javaを作成しましょう。
(クラス図)
package:jp.ict.aso.model
--------------------------------------  //クラス名
CalendarHtmlBean                     
--------------------------------------  //フィールド(プロパティ)
- year:int                                      //入力年    
- month:int                                   //入力月        
- resultCalendar:String             //結果html 
--------------------------------------  //メソッド
+ CalendarHtmlBean():    
+ CalendarHtmlBean(year:int,month:int):    
+ createCalendar():void             //カレンダー作成
+ getResultCalendar():String      //カレンダー取得
+ getYear():int          //対象年取得
+ getMonth():int        //対象月取得
--------------------------------------
-:private
+:public
#:protected

CalendarHtmlBean.java
CalendarHtmlBean.pdf


(3)カレンダー部品(Bean)をテストするJSPを作成する
calendarTest.jspを作成してWebでBeanの実行を確認しましょう。MVパターンで作成しますのでJSPファイルの保存場所に注意してください。
nullが出ていませんか?適切に対応してください!
calendarTest.jsp
calendarTest.pdf

【実行結果】

(4)カレンダーBeanを拡張してスケジュール機能を加える
CalendarHtmlBean.javaを継承したCalendarScheduleHtmlBean.javaを作成しましょう。
(クラス図)
package:jp.ict.aso.model
--------------------------------------  //クラス名
CalendarHtmlBean                     
--------------------------------------  //フィールド(プロパティ)
- year:int                                      //入力年    
- month:int                                   //入力月        
- resultCalendar:String             //結果html 
--------------------------------------  //メソッド
+ CalendarHtmlBean():    
+ CalendarHtmlBean(year:int,month:int):    
+ createCalendar():void             //カレンダー作成
+ getResultCalendar():String      //カレンダー取得
+ getYear():int          //対象年取得
+ getMonth():int        //対象月取得
--------------------------------------
↑extends //継承
--------------------------------------  //クラス名
CalendarScheduleHtmlBean                     
--------------------------------------  //フィールド(プロパティ)
- resultSchedule:String             //結果html 
--------------------------------------  //メソッド
+ CalendarScheduleHtmlBean():    
+ createSchedule():void             //スケジュール作成
+ getResultSchedule():String      //スケジュール取得
--------------------------------------
-:private
+:public
#:protected

CalendarScheduleHtmlBean.java

CalendarScheduleHtmlBean.pdf


(5)スケジュール部品(Bean)をテストするJSPを作成する
calenScheTest.jspを作成してWebでBeanの実行を確認しましょう。MVパターンで作成しますのでJSPファイルの保存場所に注意してください。
※calendarTest.jspの内容をコピーして変更すると効率的です
calenScheTest.jsp
calenScheTest.pdf

【実行結果】

(6)MVCパターンで実装するための内部設計書を作成します



(7)Beanの動きを制御するServletを作成する
MVCパターンで実行させるためCalendarScheduleServlet.javaを作成しましょう。
CalendarScheduleServlet.java
CalendarScheduleServlet.pdf


(8)カレンダーとスケジュールの表示を行うJSP(View)を作成する
calendarScheduleHtml.jspを作成してWebでBeanの実行を確認しましょう。MVCパターンで作成しますのでJSPファイルの保存場所に注意してください。
※calenScheTest.jspの内容をコピーして変更すると効率的です
calendarScheduleHtml.jsp
calendarScheduleHtml.pdf

【実行結果】

最後に時間があればJSPの表示を飾ってください!