解答例 - 実習課題1 - 10.Nestedタグライブラリ
(実習課題1)
以下のWebアプリケーションを作成しなさい。
- 世界各国の一覧を表示しなさい。少なくとも5つ以上の国を表示する事。またそれぞれの国に関する情報を、少なくとも3つは表示する事。
- 国の情報の管理はJava Beanで行う事。
- Nestedタグライブラリを利用する事。
解答例
▼ディレクトリ構成は以下の通り
. ├─com │ └─techscore │ └─struts │ └─chapter10 │ └─exercise1 useNestedTag.jsp └─WEB-INF web.xml,struts-config.xml ├─classes │ └─com │ └─techscore │ └─struts EncodingFilter.class(2章 実習課題2と同じ), │ │ MessageResources.properties(中身は空) │ └─chapter10 │ └─exercise1 Country.class ├─lib strutsライブラリjarファイル └─tld struts-bean.tld,struts-html.tld,struts-logic.tld,struts-nested.tld ※strutsライブラリjarファイル struts.jar,commons-beanutils.jar,commons-collections.jar,commons-digester.jar,commons-logging.jar
/** * Country.java * TECHSCORE Java JakartaStruts 10章 実習課題1 * * Copyright (c) 2004 Four-Dimensional Data, Inc. * */ package com.techscore.struts.chapter10.exercise1; public class Country { private String name; private int rank; private int[] medal = {0, 0, 0, 0}; public Country(String name, int rank, int[] medal) { this.name = name; this.rank = rank; this.medal = medal; } public void setName(String name){ this.name = name; } public String getName(){ return name; } public void setRank(int rank){ this.rank = rank; } public int getRank(){ return rank; } public void setMedal(int[] medal){ this.medal = medal; } public int[] getMedal(){ return medal; } }
<!-- useNestedTag.jsp --> <!-- TECHSCORE Java JakartaStruts 10章 実習課題1 --> <!-- Copyright (c) 2004 Four-Dimensional Data, Inc. --> <%@ page contentType="text/html; charset=Windows-31J" session="false" pageEncoding="Windows-31J" import="com.techscore.struts.chapter10.exercise1.Country" %> <%@ taglib uri="http://jakarta.apache.org/struts/tags-bean" prefix="bean" %> <%@ taglib uri="http://jakarta.apache.org/struts/tags-html" prefix="html" %> <%@ taglib uri="http://jakarta.apache.org/struts/tags-logic" prefix="logic" %> <%@ taglib uri="http://jakarta.apache.org/struts/tags-nested" prefix="nested" %> <html:html> <head><title>TECHSCORE Java JakartaStruts 10章 実習課題1</title></head> <body> <% int[][] medal = {{35, 39, 29, 103}, {32, 17, 14, 63}, {27, 27, 38, 92}, {17, 16, 16, 49}, {16, 9, 12, 37}}; Country[] countries = new Country[5]; countries[0] = new Country("アメリカ", 1, medal[0]); countries[1] = new Country("中国", 2, medal[1]); countries[2] = new Country("ロシア", 3, medal[2]); countries[3] = new Country("オーストラリア", 4, medal[3]); countries[4] = new Country("日本", 5, medal[4]); %> <h3>2004アテネオリンピックメダル獲得枚数</h3> <table border="2"> <tr><td>順位</td><td>国名</td><td>金</td><td>銀</td><td>銅</td><td>計</td></tr> <logic:iterate collection="<%=countries %>" id="country"> <nested:root name="country"> <tr><td><nested:write property="rank" /></td> <td><nested:write property="name" /></td> <td><nested:write property="medal[0]" /></td> <td><nested:write property="medal[1]" /></td> <td><nested:write property="medal[2]" /></td> <td><nested:write property="medal[3]" /></td></tr> </nested:root> </logic:iterate> </table> </body> </html:html>
<?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> <message-resources parameter="com.techscore.struts.MessageResources" /> </struts-config>
<?xml version="1.0" encoding="Windows-31J" ?> <!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/j2ee/dtds/web-app_2_3.dtd"> <web-app> <filter> <filter-name>Encoding</filter-name> <filter-class>com.techscore.struts.EncodingFilter</filter-class> <init-param> <param-name>encoding</param-name> <param-value>Windows-31J</param-value> </init-param> </filter> <filter-mapping> <filter-name>Encoding</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <servlet> <servlet-name>Action</servlet-name> <servlet-class>org.apache.struts.action.ActionServlet</servlet-class> <init-param> <param-name>config</param-name> <param-value>/WEB-INF/struts-config.xml</param-value> </init-param> <init-param> <param-name>debug</param-name> <param-value>6</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>Action</servlet-name> <url-pattern>*.do</url-pattern> </servlet-mapping> <taglib> <taglib-uri>http://jakarta.apache.org/struts/tags-bean</taglib-uri> <taglib-location>/WEB-INF/tld/struts-bean.tld</taglib-location> </taglib> <taglib> <taglib-uri>http://jakarta.apache.org/struts/tags-html</taglib-uri> <taglib-location>/WEB-INF/tld/struts-html.tld</taglib-location> </taglib> <taglib> <taglib-uri>http://jakarta.apache.org/struts/tags-logic</taglib-uri> <taglib-location>/WEB-INF/tld/struts-logic.tld</taglib-location> </taglib> <taglib> <taglib-uri>http://jakarta.apache.org/struts/tags-nested</taglib-uri> <taglib-location>/WEB-INF/tld/struts-nested.tld</taglib-location> </taglib> </web-app>
▼起動URLは以下の通り
WEB_ROOT/com/techscore/struts/chapter10/exercise1/useNestedTag.jsp
EncodingFilter.javaの参照(2章の実習課題2と同じ)