目次へ

解答例 - 実習課題1 - 23.Tiles タグライブラリ2

(実習課題1)

19章の実習課題2の Web アプリケーションを改良しなさい。

  • Tiles 設定ファイルを使用しないこと。

解答例

▼ディレクトリ構成は以下の通り

.
└─WEB-INF              web.xml,struts-config.xml
    ├─classes
    │  └─com
    │      └─techscore
    │          └─struts       EncodingFilter.class(2章 実習課題2と同じ),
    ├─JSP
    │  └─com
    │      └─techscore
    │          └─struts
    │              └─chapter23
    │                  └─exercise1 content1.jsp,content2.jsp,menu.jsp,useTilesInsert.jsp,tilesConf1.jsp,tilesConf2.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 23章 実習課題1 -->
<!-- Copyright (c) 2004 Four-Dimensional Data, Inc. -->

<%@ page pageEncoding="Windows-31J" %>

<p align="center">コンテンツその1</p>
<!-- content2.jsp -->
<!-- TECHSCORE Java JakartaStruts 23章 実習課題1 -->
<!-- Copyright (c) 2004 Four-Dimensional Data, Inc. -->

<%@ page pageEncoding="Windows-31J" %>

<p align="center">コンテンツその2</p>
<!-- menu.jsp -->
<!-- TECHSCORE Java JakartaStruts 23章 実習課題1 -->
<!-- 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/chapter23/exercise1/useTilesInsert1.do">
        コンテンツ1
    </html:link>
</p>
<p align="center">
    <html:link page="/com/techscore/struts/chapter23/exercise1/useTilesInsert2.do">
        コンテンツ2
    </html:link>
</p>
<!-- tilesConf1.jsp -->
<!-- TECHSCORE Java JakartaStruts 23章 実習課題1 -->
<!-- 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-tiles"
           prefix="tiles" %>

<tiles:definition id="useTiles231_1"
            page="/WEB-INF/JSP/com/techscore/struts/chapter23/exercise1/useTilesInsert.jsp">
    <tiles:put name="title" value="TECHSCORE Java JakartaStruts 23章 実習課題1" />
    <tiles:put name="menu" value="/WEB-INF/JSP/com/techscore/struts/chapter23/exercise1/menu.jsp" />
    <tiles:put name="content" value="/WEB-INF/JSP/com/techscore/struts/chapter23/exercise1/content1.jsp" />
    <tiles:put name="copyright" value="Copyright (c) 2004 Four-Dimensional Data, Inc." />
</tiles:definition>

<tiles:insert beanName="useTiles231_1" flush="true"/>
<!-- tilesConf2.jsp -->
<!-- TECHSCORE Java JakartaStruts 23章 実習課題1 -->
<!-- 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-tiles"
           prefix="tiles" %>

<tiles:definition id="useTiles231_2"
            page="/WEB-INF/JSP/com/techscore/struts/chapter23/exercise1/useTilesInsert.jsp">
    <tiles:put name="title" value="title change test" />
    <tiles:put name="menu" value="/WEB-INF/JSP/com/techscore/struts/chapter23/exercise1/menu.jsp" />
    <tiles:put name="content" value="/WEB-INF/JSP/com/techscore/struts/chapter23/exercise1/content2.jsp" />
    <tiles:put name="copyright" value="copyright change test" />
</tiles:definition>

<tiles:insert beanName="useTiles231_2" flush="true"/>
<!-- useTilesInsert.jsp -->
<!-- TECHSCORE Java JakartaStruts 23章 実習課題1 -->
<!-- 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 23章 実習課題1</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>

▼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/chapter23/exercise1/useTilesInsert1" 
			    type="org.apache.struts.actions.ForwardAction" 
			    parameter="/WEB-INF/JSP/com/techscore/struts/chapter23/exercise1/tilesConf1.jsp"/>
			
		<action path="/com/techscore/struts/chapter23/exercise1/useTilesInsert2" 
			    type="org.apache.struts.actions.ForwardAction" 
			    parameter="/WEB-INF/JSP/com/techscore/struts/chapter23/exercise1/tilesConf2.jsp"/>

    </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>

▼web.xml

<?xml version="1.0" encoding="Windows-31J" ?>
<!DOCTYPE web-app PUBLIC
  "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
  "http://java.sun.com/j2ee/dtds/web-app_2_3.dtd">

<!-- Copyright (c) 2002 by ObjectLearn. All Rights Reserved. -->
<web-app>

    <filter>
        <filter-name>Encoding</filter-name>
        <filter-class>com.techscore.struts.EncodingFilter</filter-class>
        <init-param>
            <param-name>encoding</param-name>
            <param-value>Windows-31J</param-value>
        </init-param>
    </filter>
    
    <filter-mapping>
        <filter-name>Encoding</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

    <servlet>
        <servlet-name>Action</servlet-name>
        <servlet-class>org.apache.struts.action.ActionServlet</servlet-class>
        <init-param>
            <param-name>config</param-name>
            <param-value>/WEB-INF/struts-config.xml</param-value>
        </init-param>
        <init-param>
            <param-name>debug</param-name>
            <param-value>6</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>Action</servlet-name>
        <url-pattern>*.do</url-pattern>
    </servlet-mapping>

    <taglib>
        <taglib-uri>http://jakarta.apache.org/struts/tags-bean</taglib-uri>
        <taglib-location>/WEB-INF/tld/struts-bean.tld</taglib-location>
    </taglib>

    <taglib>
        <taglib-uri>http://jakarta.apache.org/struts/tags-html</taglib-uri>
        <taglib-location>/WEB-INF/tld/struts-html.tld</taglib-location>
    </taglib>

    <taglib>
        <taglib-uri>http://jakarta.apache.org/struts/tags-logic</taglib-uri>
        <taglib-location>/WEB-INF/tld/struts-logic.tld</taglib-location>
    </taglib>

    <taglib>
        <taglib-uri>http://jakarta.apache.org/struts/tags-tiles</taglib-uri>
        <taglib-location>/WEB-INF/tld/struts-tiles.tld</taglib-location>
    </taglib>

    <taglib>
        <taglib-uri>http://www.4dd.co.jp/tags/original</taglib-uri>
        <taglib-location>/WEB-INF/tld/original.tld</taglib-location>
    </taglib>

</web-app>

▼起動URLは以下の通り

WEB_ROOT/com/techscore/struts/chapter23/exercise1/useTilesInsert1.do
WEB_ROOT/com/techscore/struts/chapter23/exercise1/useTilesInsert2.do

EncodingFilter.javaの参照(2章の実習課題2と同じ)


↑このページの先頭へ

こちらもチェック!

PR
  • XMLDB.jp
  • シナジーマーケティング研究開発グループブログ