解答例 - 実習課題2 - 22.Tilesタグライブラリ
(実習課題2)
19章の実習課題3で作成した Web アプリケーションを改良しなさい。
- insert タグの beanName, name 属性を使用し、page, attribute 属性は使用しないこと。
解答例
▼ディレクトリ構成は以下の通り
. └─WEB-INF web.xml(22章 実習課題1と同じ),struts-config.xml,tiles-defs.xml ├─classes │ └─com │ └─techscore │ └─struts EncodingFilter.class(2章 実習課題2と同じ), ├─JSP │ └─com │ └─techscore │ └─struts │ └─chapter22 │ └─exercise2 content1.jsp,content2.jsp,menu.jsp,useTilesInsert.jsp ├─lib strutsライブラリ jarファイル └─tld struts-tiles.tld,struts-html.tld ※strutsライブラリjarファイル struts.jar,commons-beanutils.jar,commons-collections.jar,commons-digester.jar,commons-logging.jar,jakarta-oro.jar
<!-- content1.jsp --> <!-- TECHSCORE Java JakartaStruts 22章 実習課題2 --> <!-- Copyright (c) 2004 Four-Dimensional Data, Inc. --> <%@ page pageEncoding="Windows-31J" %> <p align="center">コンテンツその1</p>
<!-- content2.jsp --> <!-- TECHSCORE Java JakartaStruts 22章 実習課題2 --> <!-- Copyright (c) 2004 Four-Dimensional Data, Inc. --> <%@ page pageEncoding="Windows-31J" %> <p align="center">コンテンツその2</p>
<!-- menu.jsp --> <!-- TECHSCORE Java JakartaStruts 22章 実習課題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-html" prefix="html" %> <p align="center">メニュー</p> <p align="center"> <html:link page="/com/techscore/struts/chapter22/exercise2/useTilesInsert1.do"> コンテンツ1 </html:link> </p> <p align="center"> <html:link page="/com/techscore/struts/chapter22/exercise2/useTilesInsert2.do"> コンテンツ2 </html:link> </p> <p align="center"> <html:link page="/com/techscore/struts/chapter22/exercise2/useTilesInsert3.do"> コンテンツ2−2 </html:link> </p>
<!-- useTilesInsert.jsp --> <!-- TECHSCORE Java JakartaStruts 22章 実習課題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-html" prefix="html" %> <%@ taglib uri="http://jakarta.apache.org/struts/tags-tiles" prefix="tiles" %> <html:html> <head><title>TECHSCORE Java JakartaStruts 22章 実習課題2</title></head> <body> <table border="1" width="100%"> <!-- ヘッダー部分--> <tr><td colspan="2"><p align="center"><tiles:getAsString name="title" /></p></td></tr> <tr> <!-- メニュー部分--> <td width="20%"><tiles:insert name="menu" /></td> <!-- コンテンツ部分--> <td><tiles:insert name="content" /></td> </tr> <!-- フッター部分--> <tr><td colspan="2"><p align="center"><tiles:getAsString name="copyright" /></p></td></tr> </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/chapter22/exercise2/useTilesInsert1" forward="useTiles222_1" /> <action path="/com/techscore/struts/chapter22/exercise2/useTilesInsert2" forward="useTiles222_2" /> <action path="/com/techscore/struts/chapter22/exercise2/useTilesInsert3" forward="useTiles222_3" /> </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="useTiles222_1" path="/WEB-INF/JSP/com/techscore/struts/chapter22/exercise2/useTilesInsert.jsp"> <put name="title" value="TECHSCORE Java JakartaStruts 22章 実習課題2" /> <put name="menu" value="/WEB-INF/JSP/com/techscore/struts/chapter22/exercise2/menu.jsp" /> <put name="content" value="/WEB-INF/JSP/com/techscore/struts/chapter22/exercise2/content1.jsp" /> <put name="copyright" value="Copyright (c) 2004 Four-Dimensional Data, Inc." /> </definition> <definition name="useTiles222_2" path="/WEB-INF/JSP/com/techscore/struts/chapter22/exercise2/useTilesInsert.jsp"> <put name="title" value="title change test" /> <put name="menu" value="/WEB-INF/JSP/com/techscore/struts/chapter22/exercise2/menu.jsp" /> <put name="content" value="/WEB-INF/JSP/com/techscore/struts/chapter22/exercise2/content2.jsp" /> <put name="copyright" value="copyright change test" /> </definition> <definition name="useTiles222_3" extends="useTiles222_2"> <put name="title" value="extends test" /> </definition> </tiles-definitions>
▼起動URLは以下の通り
WEB_ROOT/com/techscore/struts/chapter22/exercise2/useTilesInsert1.do WEB_ROOT/com/techscore/struts/chapter22/exercise2/useTilesInsert2.do WEB_ROOT/com/techscore/struts/chapter22/exercise2/useTilesInsert3.do
EncodingFilter.javaの参照(2章の実習課題2と同じ)