解答例 - 実習課題2 - 10.Nestedタグライブラリ
(実習課題2)
実習課題1のアプリケーションを改良しなさい。
- それぞれの国の首都の情報を追加して表示するようにする事。それぞれの首都に関する情報を少なくとも3つは表示する事。
- 首都の情報の管理は、Java Beanで行う事。
- Nestedタグライブラリを使用すること。
解答例
▼ディレクトリ構成は以下の通り
.
├─com
│ └─techscore
│ └─struts
│ └─chapter10
│ └─exercise2 useNestedTag.jsp
└─WEB-INF web.xml(実習課題1と同じ),struts-config.xml(実習課題1と同じ)
├─classes
│ └─com
│ └─techscore
│ └─struts EncodingFilter.class(2章 実習課題2と同じ),
│ │ MessageResources.properties(中身は空)
│ └─chapter10
│ └─exercise2 Country.class,Capital.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章 実習課題2
*
* Copyright (c) 2004 Four-Dimensional Data, Inc.
*
*/
package com.techscore.struts.chapter10.exercise2;
import com.techscore.struts.chapter10.exercise2.Capital;
public class Country {
private String name;
private int lank;
private int[] medal = {0, 0, 0, 0};
private Capital capital;
public Country(String name, int lank, int[] medal, Capital capital) {
this.name = name;
this.lank = lank;
this.medal = medal;
this.capital = capital;
}
public void setName(String name){
this.name = name;
}
public String getName(){
return name;
}
public void setLank(int lank){
this.lank = lank;
}
public int getLank(){
return lank;
}
public void setMedal(int[] medal){
this.medal = medal;
}
public int[] getMedal(){
return medal;
}
public void setCapital(Capital capital){
this.capital = capital;
}
public Capital getCapital(){
return capital;
}
}
/**
* Capital.java
* TECHSCORE Java JakartaStruts 10章 実習課題2
*
* Copyright (c) 2004 Four-Dimensional Data, Inc.
*
*/
package com.techscore.struts.chapter10.exercise2;
public class Capital {
private String name;
private int population;
private int timeDifference;
public Capital(String name, int population, int timeDifference) {
this.name = name;
this.population = population;
this.timeDifference = timeDifference;
}
public void setName(String name){
this.name = name;
}
public String getName(){
return name;
}
public void setPopulation(int population){
this.population = population;
}
public int getPopulation(){
return population;
}
public void setTimeDifference(int timeDifference){
this.timeDifference = timeDifference;
}
public int getTimeDifference(){
return timeDifference;
}
}
<!-- useNestedTag.jsp -->
<!-- TECHSCORE Java JakartaStruts 10章 実習課題2 -->
<!-- Copyright (c) 2004 Four-Dimensional Data, Inc. -->
<%@ page contentType="text/html; charset=Windows-31J"
session="false"
pageEncoding="Windows-31J"
import="com.techscore.struts.chapter10.exercise2.Country,
com.techscore.struts.chapter10.exercise2.Capital" %>
<%@ 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章 実習課題2</title></head>
<body>
<% Capital[] capitals = new Capital[5];
capitals[0] = new Capital("ワシントン", 61, -14);
capitals[1] = new Capital("北京", 1036, -1);
capitals[2] = new Capital("モスクワ", 875, -6);
capitals[3] = new Capital("キャンベラ", 28, 1);
capitals[4] = new Capital("東京", 798, 0);
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], capitals[0]);
countries[1] = new Country("中国", 2, medal[1], capitals[1]);
countries[2] = new Country("ロシア", 3, medal[2], capitals[2]);
countries[3] = new Country("オーストラリア", 4, medal[3], capitals[3]);
countries[4] = new Country("日本", 5, medal[4], capitals[4]); %>
<h3>2004アテネオリンピックメダル獲得枚数と各国首都データ</h3>
<table border="2">
<tr><td>順位</td><td>国名</td><td>金</td><td>銀</td><td>銅</td><td>計</td>
<td>首都名</td><td>首都人口(万人)</td><td>日本との時差(時間)</td></tr>
<logic:iterate collection="<%=countries %>" id="country" indexId="index">
<nested:root name="country">
<tr><td><nested:write property="lank" /></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>
<nested:nest property="capital">
<td><nested:write property="name" /></td>
<td><nested:write property="population" /></td>
<td><nested:write property="timeDifference" /></td>
</nested:nest>
</tr>
</nested:root>
</logic:iterate>
</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>
<message-resources parameter="com.techscore.struts.MessageResources" />
</struts-config>
▼起動URLは以下の通り
WEB_ROOT/com/techscore/struts/chapter10/exercise2/useNestedTag.jsp
struts-config.xmlの参照(実習課題1と同じ)
EncodingFilter.javaの参照(2章の実習課題2と同じ)

