解答例 - 実習課題2 - 8.Logicタグライブラリ
(実習課題2)
実習課題1のプログラムを改良しなさい。
- 5の行の色を変える事。
- 2以下の列の色も変えること。
- logicタグライブラリのタグを用いて実現する事。
解答例
▼ディレクトリ構成は以下の通り
.
├─com
│ └─techscore
│ └─struts
│ └─chapter8
│ └─exercise2 kuku.jsp
└─WEB-INF web.xml(実習課題1と同じ),struts-config.xml(実習課題1と同じ)
├─classes
│ └─com
│ └─techscore
│ └─struts EncodingFilter.class(2章 実習課題2と同じ),
│ MessageResources.properties(中身は空)
├─lib strutsライブラリjarファイル
└─tld struts-bean.tld,struts-html.tld,struts-logic.tld
※strutsライブラリjarファイル
struts.jar,commons-beanutils.jar,commons-collections.jar,commons-digester.jar,commons-logging.jar
<!-- kuku.jsp -->
<!-- TECHSCORE Java JakartaStruts 8章 実習課題2 -->
<!-- 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" %>
<%@ taglib uri="http://jakarta.apache.org/struts/tags-logic"
prefix="logic" %>
<html:html>
<head><title>TECHSCORE Java JakartaStruts 8章 実習課題2</title></head>
<body>
<%! int count = 9; %>
<% int[][] kuku = new int[9][9];
for(int i = 0; i < count; i++) {
for(int j = 0; j < count; j++) {
kuku[i][j] = (i + 1) * (j + 1);
}
} %>
<h1>九九表</h1>
<table border="2">
<tr><td></td><!-- 表の左上は空 -->
<!-- 1行目は文字と背景の色を変えて、1の段の値を使って表示 -->
<logic:iterate id="kuku_index" collection="<%=kuku[0] %>" indexId="index">
<td bgcolor="yellow"><font color="red"><bean:write name="kuku_index" /></font></td>
</logic:iterate>
</tr>
<!-- 2行目以降は1列目のみ文字と背景の色を変えて、1列目の値を使って表示 -->
<logic:iterate id="kuku_row" collection="<%=kuku %>" indexId="row">
<tr><td bgcolor="yellow"><font color="red"><%=kuku[row.intValue()][0] %></font></td>
<logic:iterate id="kuku_column" name="kuku_row" indexId="column">
<!-- 5の行は背景の色を変えて表示 -->
<logic:equal name="row" value="4">
<td bgcolor="orange"><bean:write name="kuku_column" /></td>
</logic:equal>
<logic:notEqual name="row" value="4">
<!-- 2以下の列は背景の色を変えて表示 -->
<logic:lessEqual name="column" value="1">
<td bgcolor="pink"><bean:write name="kuku_column" /></td>
</logic:lessEqual>
<logic:greaterThan name="column" value="1">
<td><bean:write name="kuku_column" /></td>
</logic:greaterThan>
</logic:notEqual>
</logic:iterate>
</tr>
</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/chapter8/exercise2/kuku.jsp
struts-config.xmlの参照(実習課題1と同じ)
EncodingFilter.javaの参照(2章の実習課題2と同じ)

