解答例 - 実習課題4 - 6.HTMLタグライブラリ2
(実習課題4)
以下のWebアプリケーションを、Strutsを用いて作成しなさい。
- checkboxタグとmultiboxタグのそれぞれを使用したHTMLフォームを作成する事。
- multiboxタグのパラメータを受け取るActionForm Beanのプロパティはint型配列とする事
解答例
▼ディレクトリ構成は以下の通り
. ├─com │ └─techscore │ └─struts │ └─chapter6 │ └─exercise4 useCheckBoxTag.jsp,displayChecked.jsp └─WEB-INF web.xml(実習課題1と同じ),struts-config.xml ├─classes │ └─com │ └─techscore │ └─struts EncodingFilter.class(2章 実習課題2と同じ), │ └─chapter6 │ └─exercise4 CheckBoxTagForm.class,CheckBoxTagAction.class ├─lib strutsライブラリjarファイル └─tld struts-bean.tld,struts-html.tld ※strutsライブラリjarファイル struts.jar,commons-beanutils.jar,commons-collections.jar,commons-digester.jar,commons-logging.jar
/** * CheckBoxTagAction.java * TECHSCORE Java JakartaStruts 6章 実習課題4 * * Copyright (c) 2004 Four-Dimensional Data, Inc. * */ package com.techscore.struts.chapter6.exercise4; import org.apache.struts.action.Action; import org.apache.struts.action.ActionMapping; import org.apache.struts.action.ActionForm; import org.apache.struts.action.ActionForward; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class CheckBoxTagAction extends Action{ public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception{ return mapping.findForward("display"); } }
/** * CheckBoxTagForm.java * TECHSCORE Java JakartaStruts 6章 実習課題4 * * Copyright (c) 2004 Four-Dimensional Data, Inc. * */ package com.techscore.struts.chapter6.exercise4; import org.apache.struts.action.ActionForm; public class CheckBoxTagForm extends ActionForm{ private boolean join = false; private int[] day; public void setJoin(boolean join){ this.join = join; } public boolean getJoin(){ return join; } public void setDay(int[] day){ this.day = day; } public int[] getDay(){ return day; } }
<!-- displayChecked.jsp --> <!-- TECHSCORE Java JakartaStruts 6章 実習課題4 --> <!-- Copyright (c) 2004 Four-Dimensional Data, Inc. --> <%@ page contentType="text/html; charset=Windows-31J" session="false" pageEncoding="Windows-31J" %> <%@ taglib uri="http://jakarta.apache.org/struts/tags-bean" prefix="bean" %> <%@ taglib uri="http://jakarta.apache.org/struts/tags-html" prefix="html" %> <html> <head><title>TECHSCORE Java JakartaStruts 6章 実習課題4</title></head> <body> <p>登録内容</p> <jsp:useBean id="checkBoxTagForm" class="com.techscore.struts.chapter6.exercise4.CheckBoxTagForm" scope="request" /> <p>参加意思有無 : <% if(checkBoxTagForm.getJoin()) { %> あり</p> <% } else { %> なし</p> <% } %> <p>参加可能日 : <% int[] day = checkBoxTagForm.getDay(); if(day == null) { %> なし <% } else { for (int i = 0; i < day.length; i++) { if (i >= 1) { %> , <% } %> <bean:write name="checkBoxTagForm" scope="request" property="<%=\"day[\" + i + \"]\" %>" /> <% } } %> </p> </body> </html>
<!-- useCheckBoxTag.jsp --> <!-- TECHSCORE Java JakartaStruts 6章 実習課題4 --> <!-- Copyright (c) 2004 Four-Dimensional Data, Inc. --> <%@ page contentType="text/html; charset=Windows-31J" session="false" pageEncoding="Windows-31J" %> <%@ taglib uri="http://jakarta.apache.org/struts/tags-html" prefix="html" %> <html> <head><title>TECHSCORE Java JakartaStruts 6章 実習課題4</title></head> <body> <p>以下を選択して登録ボタンを押してください。</p> <html:form action="/com/techscore/struts/chapter6/exercise4/CheckBoxTag.do" method="post"> <p><html:checkbox property="join" value="true" />参加意思あり</p> <p>参加可能な日 <html:multibox property="day" value="7" />7 <html:multibox property="day" value="14" />14 <html:multibox property="day" value="21" />21 <html:multibox property="day" value="28" />28</p> <p><html:submit value="登録" /></p> </html:form> </body> </html>
▼struts-config.xml
<?xml version="1.0" encoding="Windows-31J" ?> <!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.1//EN" "http://jakarta.apache.org/struts/dtds/struts-config_1_1.dtd"> <struts-config> <form-beans> <form-bean name="checkBoxTagForm" type="com.techscore.struts.chapter6.exercise4.CheckBoxTagForm" /> </form-beans> <action-mappings> <action path="/com/techscore/struts/chapter6/exercise4/CheckBoxTag" type="com.techscore.struts.chapter6.exercise4.CheckBoxTagAction" name="checkBoxTagForm" scope="request"> <forward name="display" path="/com/techscore/struts/chapter6/exercise4/displayChecked.jsp" /> </action> </action-mappings> </struts-config>
▼起動URLは以下の通り
WEB_ROOT/com/techscore/struts/chapter6/exercise4/useCheckBoxTag.jsp
EncodingFilter.javaの参照(2章の実習課題2と同じ)