解答例 - 実習課題3 - 23.Tiles タグライブラリ2
2006.02.21 株式会社四次元データ 廣澤啓史
(実習課題3)
20章の実習課題1の Web アプリケーションを改良しなさい。
- Tiles 設定ファイルを使用しないこと。
解答例
▼ディレクトリ構成は以下の通り
. └─WEB-INF web.xml,struts-config.xml ├─classes │ └─com │ └─techscore │ └─struts EncodingFilter.class(2章 実習課題2と同じ), │ │ MessageResources.properties(空ファイル) │ └─chapter20 │ │ └─exercise1 CalendarTag.class(20章 実習課題1と同じ) │ │ │ └─chapter23 │ └─exercise3 SelectMenuController.class,SelectMenuForm.class │ ├─JSP │ └─com │ └─techscore │ └─struts │ └─chapter23 │ └─exercise3 calendar.jsp,calendarPlus.jsp,clock.jsp,weather.jsp │ tilesConf1.jsp.jsp,tilesConf2.jsp.jsp,tilesConf3.jsp ├─lib strutsライブラリ jarファイル └─tld original.tld(20章 実習課題1と同じ) struts-tiles.tld,struts-html.tld,struts-logic.tld,struts-bean.tld ※strutsライブラリjarファイル struts.jar,commons-beanutils.jar,commons-collections.jar,commons-digester.jar,commons-logging.jar,jakarta-oro.jar
<!-- calendar.jsp --> <!-- TECHSCORE Java JakartaStruts 23章 実習課題3 --> <!-- Copyright (c) 2004 Four-Dimensional Data, Inc. --> <%@ page pageEncoding="Windows-31J" import="java.util.Calendar" %> <%@ taglib uri="http://www.4dd.co.jp/tags/original" prefix="original" %> <h2 align="center">カレンダー</h2> <% Calendar calendar = Calendar.getInstance(); %> <original:Calendar year="<%=Integer.toString(calendar.get(Calendar.YEAR)) %>" month="<%=Integer.toString(calendar.get(Calendar.MONTH) + 1) %>" />
<!-- calendarPlus.jsp --> <!-- TECHSCORE Java JakartaStruts 23章 実習課題3 --> <!-- 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" %> <%@ taglib uri="http://jakarta.apache.org/struts/tags-bean" prefix="bean" %> <%@ taglib uri="http://jakarta.apache.org/struts/tags-logic" prefix="logic" %> <%@ taglib uri="http://jakarta.apache.org/struts/tags-tiles" prefix="tiles" %> <html:html> <head><title>TECHSCORE Java JakartaStruts 23章 実習課題3</title></head> <body> <table border="1" width="100%"> <tr> <!-- メニュー部分--> <td align="center" width="30%"> <h3>MENU</h3> <tiles:importAttribute name="link1" /> <bean:define id="linkPath1" name="link1" property="value" type="java.lang.String"/> <p><html:link page="<%=linkPath1 %>" > <tiles:importAttribute name="name1" /> <bean:write name="name1" /> </html:link></p> <tiles:importAttribute name="link2" /> <bean:define id="linkPath2" name="link2" property="value" type="java.lang.String"/> <p><html:link page="<%=linkPath2 %>" > <tiles:importAttribute name="name2" /> <bean:write name="name2" /> </html:link></p> </td> <!-- コンテンツ部分--> <td align="center"> <tiles:insert attribute="content" /> </td> </tr> </table> </body> </html:html>
<!-- clock.jsp --> <!-- TECHSCORE Java JakartaStruts 23章 実習課題3 --> <!-- Copyright (c) 2004 Four-Dimensional Data, Inc. --> <%@ page pageEncoding="Windows-31J" import="java.text.DateFormat, java.text.SimpleDateFormat, java.util.Date, java.util.TimeZone" %> <%@ taglib uri="http://jakarta.apache.org/struts/tags-bean" prefix="bean" %> <%@ taglib uri="http://jakarta.apache.org/struts/tags-logic" prefix="logic" %> <% String[] cities = {"Japan", "America/Los-Angeles", "America/New_York", "Europe/Moscow", "Pacific/Saipan"}; %> <h2 align="center">世界時計</h2> <% DateFormat dateFormat = new SimpleDateFormat("MM/dd HH:mm:ss"); %> <table border="1" width="100%"> <logic:iterate collection="<%=cities %>" id="city" indexId="index"> <tr><td align="center"> <bean:write name="city" /> </td> <td align="center"> <bean:define id="zoneName" name="city" type="java.lang.String" /> <% dateFormat.setTimeZone(TimeZone.getTimeZone(zoneName)); %> <%= dateFormat.format(new Date()) %> </td></tr> </logic:iterate> </table>
<!-- weather.jsp --> <!-- TECHSCORE Java JakartaStruts 23章 実習課題3 --> <!-- Copyright (c) 2004 Four-Dimensional Data, Inc. --> <%@ page pageEncoding="Windows-31J" %> <h2 align="center">天気予報</h2> <table border="1" width="100%"> <tr><td align="center">Tokyo</td><td align="center">晴れ</td></tr> <tr><td align="center">Los-Angeles</td><td align="center">晴れ時々曇り</td></tr> <tr><td align="center">New_York</td><td align="center">雨のち曇り</td></tr> <tr><td align="center">Moscow</td><td align="center">曇りのち晴れ</td></tr> <tr><td align="center">Saipan</td><td align="center">晴れのち曇り</td></tr> </table>
<!-- tilesConf1.jsp --> <!-- TECHSCORE Java JakartaStruts 23セマ シツスャインツ・ --> <!-- 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-tiles" prefix="tiles" %> <tiles:definition id="calendar233-1" page="/WEB-INF/JSP/com/techscore/struts/chapter23/exercise3/calendarPlus.jsp"> <tiles:put name="content" value="/WEB-INF/JSP/com/techscore/struts/chapter23/exercise3/calendar.jsp" /> <tiles:put name="link1" value="/com/techscore/struts/chapter23/exercise3/CalendarPlus_clock.do" type="String"/> <tiles:put name="link2" value="/com/techscore/struts/chapter23/exercise3/CalendarPlus_weather.do" type="String" /> <tiles:put name="name1" value="世界時計" /> <tiles:put name="name2" value="天気予報" /> </tiles:definition> <tiles:insert beanName="calendar233-1" flush="true" />
<!-- tilesConf2.jsp --> <!-- TECHSCORE Java JakartaStruts 23章 実習課題3 --> <!-- 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-tiles" prefix="tiles" %> <tiles:definition id="calendar233-2" page="/WEB-INF/JSP/com/techscore/struts/chapter23/exercise3/calendarPlus.jsp"> <tiles:put name="content" value="/WEB-INF/JSP/com/techscore/struts/chapter23/exercise3/clock.jsp" /> <tiles:put name="link1" value="/com/techscore/struts/chapter23/exercise3/CalendarPlus_calendar.do" type="String"/> <tiles:put name="link2" value="/com/techscore/struts/chapter23/exercise3/CalendarPlus_weather.do" type="String" /> <tiles:put name="name1" value="カレンダー" /> <tiles:put name="name2" value="天気予報" /> </tiles:definition> <tiles:insert beanName="calendar233-2" flush="true" />
<!-- tilesConf3.jsp --> <!-- TECHSCORE Java JakartaStruts 23章 実習課題3 --> <!-- 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-tiles" prefix="tiles" %> <tiles:definition id="calendar233-3" page="/WEB-INF/JSP/com/techscore/struts/chapter23/exercise3/calendarPlus.jsp"> <tiles:put name="content" value="/WEB-INF/JSP/com/techscore/struts/chapter23/exercise3/weather.jsp" /> <tiles:put name="link1" value="/com/techscore/struts/chapter23/exercise3/CalendarPlus_calendar.do" type="String"/> <tiles:put name="link2" value="/com/techscore/struts/chapter23/exercise3/CalendarPlus_clock.do" type="String" /> <tiles:put name="name1" value="カレンダー" /> <tiles:put name="name2" value="世界時計" /> </tiles:definition> <tiles:insert beanName="calendar233-3" flush="true" />
▼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> <action-mappings> <action path="/com/techscore/struts/chapter23/exercise3/CalendarPlus_calendar" type="org.apache.struts.actions.ForwardAction" parameter="/WEB-INF/JSP/com/techscore/struts/chapter23/exercise3/tilesConf1.jsp"/> <action path="/com/techscore/struts/chapter23/exercise3/CalendarPlus_clock" type="org.apache.struts.actions.ForwardAction" parameter="/WEB-INF/JSP/com/techscore/struts/chapter23/exercise3/tilesConf2.jsp"/> <action path="/com/techscore/struts/chapter23/exercise3/CalendarPlus_weather" type="org.apache.struts.actions.ForwardAction" parameter="/WEB-INF/JSP/com/techscore/struts/chapter23/exercise3/tilesConf3.jsp"/> </action-mappings> <message-resources parameter="com.techscore.struts.MessageResources" /> <plug-in className="org.apache.struts.tiles.TilesPlugin"> <set-property property="definitions-config" value="/WEB-INF/tiles-defs.xml" /> </plug-in> </struts-config>
▼起動URLは以下の通り
WEB_ROOT/com/techscore/struts/chapter23/exercise3/CalendarPlus_calendar.do
EncodingFilter.javaの参照(2章の実習課題2と同じ)
CalendarTag.javaの参照(20章の実習課題1と同じ)