シンプルな Struts アプリケーションにおける、アクションのソース・コード

package com.ibm.dayofweek;


import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionError;
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 com.ibm.dayofweek.DateData;


/**
 * @version 	1.0
 * @author
 */

public class ComputeDayAction extends Action {

	
	/**
	 * コンストラクター
	 */
	public ComputeDayAction() {

		super();

	}

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

				ActionErrors errors = new ActionErrors();
		ActionForward forward = new ActionForward();
		// 戻り値
		DateData dateData = (DateData) form;
		int year;
		int month;
		int day;
		int dayOfWeek;
		int valcen;
		int valleap;
		int valyear;
		int valmon;
		int valday;
		int[] centuries = new int[4];
		int[] months = new int[13];
		String[] daysOfWeek = new String[7];
		centuries[0] = 2;
		centuries[1] = 0;
		centuries[2] = 5;
		centuries[3] = 3;
		months[1] = 5;
		months[2] = 1;
		months[3] = 0;
		months[4] = 3;
		months[5] = 5;
		months[6] = 1;
		months[7] = 3;
		months[8] = 6;
		months[9] = 2;
		months[10] = 4;
		months[11] = 0;
		months[12] = 2;
		daysOfWeek[0] = "日曜日";
		daysOfWeek[1] = "月曜日";
		daysOfWeek[2] = "火曜日";
		daysOfWeek[3] = "水曜日";
		daysOfWeek[4] = "木曜日";
		daysOfWeek[5] = "金曜日";
		daysOfWeek[6] = "土曜日";

		try {
			day = dateData.getDay();
			month = dateData.getMonth();
			year = dateData.getYear();
			if (month < 3) {
				year--; // 年から 1 を減算
			}
			valcen = centuries[year / 100 % 4];
			valleap = year % 100 / 4;
			valyear = year % 100 % 7;
			valmon = months[month];
			valday = day % 7;
			dayOfWeek = valcen + valleap + valyear + valmon + valday;
			dayOfWeek = dayOfWeek % 7;
			dateData.setDayOfWeek(daysOfWeek[dayOfWeek]);
			request.setAttribute("dateData", dateData);

} catch (Exception e) {

			
// 適切な名前と ID を使用してエラーを報告します。
			errors.add("name", new ActionError("id"));
			e.printStackTrace();
		}

		// メッセージが必要とされる場合は、<struts:errors> タグ
		// で使用される指定のキーを、リクエストに保管します。
		if (!errors.isEmpty()) {
			saveErrors(request, errors);
			forward = mapping.findForward("failure");
		} else {
			forward = mapping.findForward("success");
		}

				// リターンして終了
		return (forward);

	}
}
フィードバック
(C) Copyright IBM Corporation 2000, 2005. All Rights Reserved.