解答例 - 実習課題3 - 19.レイアウト
(実習課題3)
実習課題2のWebアプリケーションを改良しなさい。
- extendsを使用し、ヘッダーの部分だけが異なる新たなページを追加すること。
解答例
▼ディレクトリ構成は以下の通り
. └─WEB-INF web.xml(19章 実習課題1と同じ),struts-config.xml,tiles-defs.xml ├─classes │ └─com │ └─techscore │ └─struts EncodingFilter.class(2章 実習課題2と同じ) ├─JSP │ └─com │ └─techscore │ └─struts │ └─chapter19 │ └─exercise3 content1.jsp,content2.jsp,menu.jsp,useTilesInsert.jsp ├─lib strutsライブラリ jarファイル └─tld struts-tiles.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 19章 実習課題3 --> <!-- Copyright (c) 2004 Four-Dimensional Data, Inc. --> <%@ page pageEncoding="Windows-31J" %> <p align="center">コンテンツその1</p>
<!-- content2.jsp --> <!-- TECHSCORE Java JakartaStruts 19章 実習課題3 --> <!-- Copyright (c) 2004 Four-Dimensional Data, Inc. --> <%@ page pageEncoding="Windows-31J" %> <p align="center">コンテンツその2</p>
<!-- menu.jsp --> <!-- TECHSCORE Java JakartaStruts 19章 実習課題3 --> <!-- 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/chapter19/exercise3/useTilesInsert1.do"> コンテンツ1 </html:link> </p> <p align="center"> <html:link page="/com/techscore/struts/chapter19/exercise3/useTilesInsert2.do"> コンテンツ2 </html:link> </p> <p align="center"> <html:link page="/com/techscore/struts/chapter19/exercise3/useTilesInsert3.do"> コンテンツ2−2 </html:link> </p>
<!-- useTilesInsert.jsp --> <!-- TECHSCORE Java JakartaStruts 19章 実習課題3 --> <!-- 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 19章 実習課題3</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 attribute="menu" /></td> <!-- コンテンツ部分--> <td><tiles:insert attribute="content" /></td> </tr> <!-- フッター部分--> <tr><td colspan="2"><p align="center"><tiles:getAsString name="copyright" /></p></td></tr> </table> </body> </html:html>
▼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="useTiles193_1" path="/WEB-INF/JSP/com/techscore/struts/chapter19/exercise3/useTilesInsert.jsp"> <put name="title" value="TECHSCORE Java JakartaStruts 19章 実習課題3" /> <put name="menu" value="/WEB-INF/JSP/com/techscore/struts/chapter19/exercise3/menu.jsp" /> <put name="content" value="/WEB-INF/JSP/com/techscore/struts/chapter19/exercise3/content1.jsp" /> <put name="copyright" value="Copyright (c) 2004 Four-Dimensional Data, Inc." /> </definition> <definition name="useTiles193_2" path="/WEB-INF/JSP/com/techscore/struts/chapter19/exercise3/useTilesInsert.jsp"> <put name="title" value="title change test" /> <put name="menu" value="/WEB-INF/JSP/com/techscore/struts/chapter19/exercise3/menu.jsp" /> <put name="content" value="/WEB-INF/JSP/com/techscore/struts/chapter19/exercise3/content2.jsp" /> <put name="copyright" value="copyright change test" /> </definition> <definition name="useTiles193_3" extends="useTiles193_2"> <put name="title" value="extends test" /> </definition> </tiles-definitions>
▼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/chapter19/exercise3/useTilesInsert1" forward="useTiles193_1" /> <action path="/com/techscore/struts/chapter19/exercise3/useTilesInsert2" forward="useTiles193_2" /> <action path="/com/techscore/struts/chapter19/exercise3/useTilesInsert3" forward="useTiles193_3" /> </action-mappings> <plug-in className="org.apache.struts.tiles.TilesPlugin"> <set-property property="definitions-config" value="/WEB-INF/tiles-defs.xml" /> </plug-in> </struts-config>
▼起動URLは以下の通り
WEB_ROOT/com/techscore/struts/chapter19/exercise3/useTilesInsert1.do WEB_ROOT/com/techscore/struts/chapter19/exercise3/useTilesInsert2.do WEB_ROOT/com/techscore/struts/chapter19/exercise3/useTilesInsert3.do
EncodingFilter.javaの参照(2章の実習課題2と同じ)