解答例 - 実習課題2 - 4.Beanタグライブラリ2
(実習課題2)
3章実習課題1のWebアプリケーションを改良しなさい。
- JavaBeansクラスにjava.util.Dateプロパティを追加する事。
- defineタグではなく、writeタグを用いて表示を行う事。
- java.util.Dateプロパティの表示には、何らかのフォーマットを指定する事。
解答例
▼ディレクトリ構成は以下の通り
.
├─com
│ └─techscore
│ └─struts
│ └─chapter4
│ └─exercise2 useTestBean.jsp
└─WEB-INF web.xml,struts-config.xml(両方とも実習課題1と同じ)
├─classes
│ └─com
│ └─techscore
│ └─struts EncodingFilter.class(2章 実習課題2と同じ),
│ │ MessageResources.properties(中身は空)
│ └─chapter4
│ └─exercise2 TestBean.class
├─lib strutsライブラリjarファイル
└─tld struts-bean.tld
※strutsライブラリjarファイル
struts.jar,commons-beanutils.jar,commons-collections.jar,commons-digester.jar,commons-logging.jar
/**
* TestBean.java
* TECHSCORE Java JakartaStruts 4章 実習課題2
*
* Copyright (c) 2004 Four-Dimensional Data, Inc.
*
*/
package com.techscore.struts.chapter4.exercise2;
import java.util.Map;
import java.util.HashMap;
import java.util.List;
import java.util.LinkedList;
import java.util.Date;
public class TestBean {
public TestBean getBean(){
return new TestBean();
}
public int getTestInt(){
int testInt = 3;
return testInt;
}
public String[] getTestString(){
String[] testString = {"文字列0", "文字列1"};
return testString;
}
public List getTestList(){
List testList = new LinkedList();
testList.add("リスト1");
testList.add("リスト2");
return testList;
}
public Map getTestMap(){
Map testMap = new HashMap();
testMap.put("mapkey0", "マップ0");
testMap.put("mapkey1", "マップ1");
return testMap;
}
public Date getTestDate(){
Date date = new Date();
return date;
}
}
<!-- useTestBean.jsp -->
<!-- TECHSCORE Java JakartaStruts 4章 実習課題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" %>
<html>
<head><title>TECHSCORE Java JakartaStruts 4章 実習課題2</title></head>
<body>
<jsp:useBean id="testBean"
class="com.techscore.struts.chapter4.exercise2.TestBean"
scope="request" />
<p>TestBean testInt :
<bean:write name="testBean" scope="request" property="testInt" /></p>
<p>TestBean testString[0] :
<bean:write name="testBean" scope="request" property="testString[0]" /></p>
<p>TestBean testString[1] :
<bean:write name="testBean" scope="request" property="testString[1]" /></p>
<p>TestBean testList[0] :
<bean:write name="testBean" scope="request" property="testList[0]" /></p>
<p>TestBean testList[1] :
<bean:write name="testBean" scope="request" property="testList[1]" /></p>
<p>TestBean testMap.mapkey0 :
<bean:write name="testBean" scope="request" property="testMap.mapkey0" /></p>
<p>TestBean testMap.mapkey1 :
<bean:write name="testBean" scope="request" property="testMap.mapkey1" /></p>
<p>TestBean testDate :
<bean:write name="testBean" scope="request"
property="testDate" format="yyyy.MM.dd" /></p>
</body></html>
▼起動URLは以下の通り
WEB_ROOT/com/techscore/struts/chapter4/exercise2/useTestBean.jsp
struts-config.xmlの参照(実習課題1と同じ)
EncodingFilter.javaの参照(2章の実習課題2と同じ)

