解答例 - 実習課題4 - 21.表示するTileの動的な変更
最終更新日 2006.02.20
(実習課題4)
実習課題3のWebアプリケーションを改良しなさい。
- 選択したTileをまとめて1つのページに表示するレイアウトに変えなさい。
- メニューは無くてよい。
解答例
▼ディレクトリ構成は以下の通り
. └─WEB-INF web.xml(21章 実習課題1と同じ),struts-config.xml,tiles-defs.xml ├─classes │ └─com │ └─techscore │ └─struts EncodingFilter.class(2章 実習課題2と同じ), │ │ MessageResources.properties(空ファイル) │ └─chapter20 │ │ └─exercise1 CalendarTag.class(20章 実習課題1と同じ) │ └─chapter21 │ └─exercise4 SelectMenuController.class,SelectMenuForm.class ├─JSP │ └─com │ └─techscore │ └─struts │ └─chapter21 │ └─exercise4 calendar.jsp,calendarPlus.jsp,clock.jsp,weather.jsp,selectMenu.jsp ├─lib strutsライブラリ jarファイル └─tld original.tld(20章 実習課題1と同じ) struts-tiles.tld,struts-html.tld,struts-logic.tld ※strutsライブラリjarファイル struts.jar,commons-beanutils.jar,commons-collections.jar,commons-digester.jar,commons-logging.jar,jakarta-oro.jar
/** * SelectMenuController.java TECHSCORE Java JakartaStruts 21章 * * Copyright (c) 2004 Four-Dimensional Data, Inc. * */ package com.techscore.struts.chapter21.exercise4; import org.apache.struts.tiles.Controller; import org.apache.struts.tiles.ComponentContext; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; import javax.servlet.ServletContext; import java.util.List; import java.util.ArrayList; public class SelectMenuController implements Controller { public void perform(ComponentContext context, HttpServletRequest request, HttpServletResponse response, ServletContext servletContext) { HttpSession session = request.getSession(); SelectMenuForm form = (SelectMenuForm) session.getAttribute("select2"); String[] menuName = form.getMenu(); String[] menuUrl = new String[3]; //メニューリストの作成 List menuList = new ArrayList(); for (int i = 0; i < menuName.length; i++) { if (menuName[i].equals("カレンダー")) { menuUrl[i] = "/WEB-INF/JSP/com/techscore/struts/chapter21/exercise4/calendar.jsp"; } if (menuName[i].equals("世界時計")) { menuUrl[i] = "/WEB-INF/JSP/com/techscore/struts/chapter21/exercise4/clock.jsp"; } if (menuName[i].equals("天気予報")) { menuUrl[i] = "/WEB-INF/JSP/com/techscore/struts/chapter21/exercise4/weather.jsp"; } menuList.add(menuUrl[i]); } //メニューリストの登録 context.putAttribute("menuList", menuList); } }
/** * SelectMenuForm.java TECHSCORE Java JakartaStruts 21章 * * Copyright (c) 2004 Four-Dimensional Data, Inc. * */ package com.techscore.struts.chapter21.exercise4; import org.apache.struts.action.ActionForm; public class SelectMenuForm extends ActionForm { String[] menu = new String[3]; public void setMenu(String[] menu) { this.menu = menu; } public String[] getMenu() { return menu; } }
<!-- calendar.jsp --> <!-- TECHSCORE Java JakartaStruts 21章 実習課題4 --> <!-- 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 21章 実習課題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-tiles" prefix="tiles" %> <%@ taglib uri="http://jakarta.apache.org/struts/tags-logic" prefix="logic" %> <%@ taglib uri="http://jakarta.apache.org/struts/tags-html" prefix="html" %> <html:html> <head><title>TECHSCORE Java JakartaStruts 21章 実習課題4</title></head> <body> <table border="1" width="100%"> <tiles:importAttribute name="menuList" /> <logic:iterate id="menuUrl" name="menuList" > <!-- コンテンツ部分--> <td align="center"> <tiles:insert beanName="menuUrl" flush="false"/> </td> </logic:iterate> </table> </body> </html:html>
<!-- clock.jsp --> <!-- TECHSCORE Java JakartaStruts 21章 実習課題4 --> <!-- 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 21章 実習課題4 --> <!-- 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>
<!-- selectMenu.jsp --> <!-- TECHSCORE Java JakartaStruts 21章 実習課題4 --> <!-- Copyright (c) 2004 Four-Dimensional Data, Inc. --> <%@ page contentType="text/html; charset=Windows-31J" session="true" pageEncoding="Windows-31J" %> <%@ taglib uri="http://jakarta.apache.org/struts/tags-html" prefix="html" %> <html:html> <head><title>セレクトメニュー</title></head> <body> メニューを選択してください<br /> <table border="1"> <html:form action="/com/techscore/struts/chapter21/exercise4/CalendarPlus2.do" method="post"> <html:select property="menu" size="3" multiple="true"> <html:option value="カレンダー">カレンダー</html:option> <html:option value="世界時計">世界時計</html:option> <html:option value="天気予報">天気予報</html:option> </html:select> <tr><td> <html:submit value="選択" /> </td></tr> </html:form> </table> </body> </html: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> <action-mappings> <action path="/com/techscore/struts/chapter21/exercise4/CalendarPlus1" type="org.apache.struts.actions.ForwardAction" parameter="/WEB-INF/JSP/com/techscore/struts/chapter21/exercise4/selectMenu.jsp" /> <action path="/com/techscore/struts/chapter21/exercise4/CalendarPlus2" type="org.apache.struts.actions.ForwardAction" name="select2" scope="session" parameter="calendarPlus214" /> </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>
▼tiles-defs.xml
<?xml version="1.0" encoding="Windows-31J" ?> <!DOCTYPE tiles-definitions PUBLIC "-//Apache Software Foundation//DTD Tiles Configuration//EN" "http://jakarta.apache.org/struts/dtds/tiles-config_1_1.dtd"> <tiles-definitions> <definition name="calendarPlus214" path="/WEB-INF/JSP/com/techscore/struts/chapter21/exercise4/calendarPlus.jsp" controllerClass="com.techscore.struts.chapter21.exercise4.SelectMenuController"> </definition> </tiles-definitions>
▼起動URLは以下の通り
WEB_ROOT/com/techscore/struts/chapter21/exercise4/CalendarPlus1.do
EncodingFilter.javaの参照(2章の実習課題2と同じ)
CalendarTag.javaの参照(20章の実習課題1と同じ)