目次へ

解答例 - 実習課題1 - 21.表示するTileの動的な変更

(実習課題1)

前章の実習課題3を改良しなさい。

  • Controllerクラスを作成し、Controllerクラスでメニューおよびタイトルのリストを設定するようにすること。

解答例

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

.
└─WEB-INF              web.xml,struts-config.xml,tiles-defs.xml
    ├─classes
    │  └─com
    │      └─techscore
    │          └─struts       EncodingFilter.class(2章 実習課題2と同じ),
    │             │            MessageResources.properties(空ファイル)
    │             └─chapter20
    │             │   └─exercise1 CalendarTag.class(20章 実習課題1と同じ)
    │             └─chapter21         
    │                  └─exercise1 MenuController.class
    ├─JSP
    │  └─com
    │      └─techscore
    │          └─struts
    │              └─chapter21
    │                  └─exercise1 calendar.jsp,calendarPlus.jsp,clock.jsp,weather.jsp,content.jsp
    ├─lib              strutsライブラリ jarファイル
    └─tld              original.tld,struts-tiles.tld,struts-html.tld,struts-beans.tld,struts-logic.tld



※strutsライブラリjarファイル
  struts.jar,commons-beanutils.jar,commons-collections.jar,commons-digester.jar,commons-logging.jar,jakarta-oro.jar
/**
 * MenuController.java TECHSCORE Java JakartaStruts 21章
 * 
 * Copyright (c) 2004 Four-Dimensional Data, Inc.
 *  
 */

package com.techscore.struts.chapter21.exercise1;

import org.apache.struts.tiles.Controller;
import org.apache.struts.tiles.ComponentContext;
import org.apache.struts.tiles.beans.SimpleMenuItem;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.ServletContext;
import java.util.List;
import java.util.ArrayList;

public class MenuController implements Controller {

    public void perform(ComponentContext context, HttpServletRequest request,
                        HttpServletResponse response,
                        ServletContext servletContext) {

        //メニューリストの作成
        List menuList = new ArrayList();
        SimpleMenuItem menu = new SimpleMenuItem();
        menu.setLink("/WEB-INF/JSP/com/techscore/struts/chapter21/execise1/calendar.jsp");
        menu.setValue("カレンダー");
        menuList.add(menu);
        menu = new SimpleMenuItem();
        menu.setLink("/WEB-INF/JSP/com/techscore/struts/chapter21/execise1/clock.jsp");
        menu.setValue("世界時計");
        menuList.add(menu);
        menu = new SimpleMenuItem();
        menu.setLink("/WEB-INF/JSP/com/techscore/struts/chapter21/execise1/weather.jsp");
        menu.setValue("天気予報");
        menuList.add(menu);

        //メニューリストの登録
        context.putAttribute("menuList", menuList);

        //タイトルリストの作成
        List titleList = new ArrayList();
        titleList.add("Calendar");
        titleList.add("Clock");
        titleList.add("Weather");

        //タイトルリストの登録
        context.putAttribute("titleList", titleList);

    }

}
<!-- calendar.jsp -->
<!-- TECHSCORE Java JakartaStruts 21章 実習課題1 -->
<!-- Copyright (c) 2004 Four-Dimensional Data, Inc. -->

<%@ page pageEncoding="Windows-31J"
         import="java.util.Calendar" %>

<%@ taglib uri="http://www.4dd.co.jp/tags/original"
    prefix="original" %>

<h2 align="center">カレンダー</h2>

<% Calendar calendar = Calendar.getInstance(); %>

<original:Calendar year="<%=Integer.toString(calendar.get(Calendar.YEAR)) %>"
                   month="<%=Integer.toString(calendar.get(Calendar.MONTH) + 1) %>" />
<!-- calendarPlus.jsp -->
<!-- TECHSCORE Java JakartaStruts 21章 実習課題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" %>
<%@ taglib uri="http://jakarta.apache.org/struts/tags-logic"
           prefix="logic" %>
<%@ taglib uri="http://jakarta.apache.org/struts/tags-bean"
           prefix="bean" %>
<%@ taglib uri="http://jakarta.apache.org/struts/tags-html"
           prefix="html" %>

<html:html>
<head>

<!-- コンテンツ番号を読み込む -->
<bean:parameter id="contentID" name="contentID" value="0" />
<tiles:importAttribute name="titleList" />
    <logic:iterate id="title" name="titleList" indexId="indexID"
                 type="java.lang.String">
        <logic:equal name="indexID" value="<%=contentID %>">
            <title><bean:write name="title"/></title>
        </logic:equal>
    </logic:iterate>

</head>

<body>

<table border="1" width="100%">
	<tr>
	    <!-- メニュー部分-->
	    <td align="center" width="30%">
            <h3>MENU</h3>
            <tiles:importAttribute name="menuList" />
            <logic:iterate id="menu" name="menuList" indexId="indexID"
                           type="org.apache.struts.tiles.beans.SimpleMenuItem">

                   <!-- 現在表示しているコンテンツ以外についてだけMENUを表示 -->
				   <logic:notEqual name="indexID" value="<%=contentID %>" >
				       <html:link href="<%=request.getRequestURL().toString() %>"
				               paramId="contentID" paramName="indexID">
				           <p><bean:write name="menu" property="value" /></p>
				       </html:link>
				   </logic:notEqual>
			</logic:iterate>
        </td>
        <!-- コンテンツ部分-->
        <td align="center">
            <tiles:insert beanName="content"/>
        </td>
    </tr>
</table>

</body>
</html:html>
<!-- clock.jsp -->
<!-- TECHSCORE Java JakartaStruts 21章 実習課題1 -->
<!-- Copyright (c) 2004 Four-Dimensional Data, Inc. -->

<%@ page pageEncoding="Windows-31J"
         import="java.text.DateFormat,
                  java.text.SimpleDateFormat,
                  java.util.Date,
                  java.util.TimeZone" %>

<%@ taglib uri="http://jakarta.apache.org/struts/tags-bean"
           prefix="bean" %>
<%@ taglib uri="http://jakarta.apache.org/struts/tags-logic"
           prefix="logic" %>

<%  String[] cities = {"Japan",
 		                   "America/Los-Angeles",
 		                   "America/New_York",
 		                   "Europe/Moscow",
 		                   "Pacific/Saipan"}; %>

<h2 align="center">世界時計</h2>

<%  DateFormat dateFormat = new SimpleDateFormat("MM/dd HH:mm:ss"); %>
<table border="1" width="100%">
    <logic:iterate collection="<%=cities %>" id="city" indexId="index">
        <tr><td align="center">
                <bean:write name="city" />
            </td>
            <td align="center">
                <bean:define id="zoneName" name="city" type="java.lang.String" />
                <% dateFormat.setTimeZone(TimeZone.getTimeZone(zoneName)); %>
                <%= dateFormat.format(new Date()) %>
            </td></tr>
    </logic:iterate>
</table>
<!-- weather.jsp -->
<!-- TECHSCORE Java JakartaStruts 21章 実習課題1 -->
<!-- Copyright (c) 2004 Four-Dimensional Data, Inc. -->

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

<h2 align="center">天気予報</h2>

<table border="1" width="100%">
    <tr><td align="center">Tokyo</td><td align="center">晴れ</td></tr>
    <tr><td align="center">Los-Angeles</td><td align="center">晴れ時々曇り</td></tr>
    <tr><td align="center">New_York</td><td align="center">雨のち曇り</td></tr>
    <tr><td align="center">Moscow</td><td align="center">曇りのち晴れ</td></tr>
    <tr><td align="center">Saipan</td><td align="center">晴れのち曇り</td></tr>
</table>
<!-- content.jsp -->
<!-- TECHSCORE Java JakartaStruts 21章 実習課題1 -->
<!-- Copyright (c) 2004 Four-Dimensional Data, Inc. -->

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

<%@ taglib uri="http://jakarta.apache.org/struts/tags-tiles"
           prefix="tiles" %>
<%@ taglib uri="http://jakarta.apache.org/struts/tags-logic"
           prefix="logic" %>

<logic:equal parameter="contentID" value="0">
    <tiles:insert page="/WEB-INF/JSP/com/techscore/struts/chapter21/exercise1/calendar.jsp" />
</logic:equal>
<logic:equal parameter="contentID" value="1">
    <tiles:insert page="/WEB-INF/JSP/com/techscore/struts/chapter21/exercise1/clock.jsp" />
</logic:equal>
<logic:equal parameter="contentID" value="2">
    <tiles:insert page="/WEB-INF/JSP/com/techscore/struts/chapter21/exercise1/weather.jsp" />
</logic:equal>
<logic:greaterEqual parameter="contentID" value="3">
    <tiles:insert page="/WEB-INF/JSP/com/techscore/struts/chapter21/exercise1/calendar.jsp" />
</logic:greaterEqual>
<logic:notPresent parameter="contentID">
    <tiles:insert page="/WEB-INF/JSP/com/techscore/struts/chapter21/exercise1/calendar.jsp" />
</logic:notPresent>

▼original.tld

<?xml version="1.0" ?>
<!DOCTYPE taglib
  PUBLIC
  "-//Sun Microsystems, Inc.//DTD JSP Tag Library 1.2//EN"
  "http://java.sun.com/dtd/web-jsptaglibrary_1_2.dtd">

<taglib>
    <tlib-version>1.0</tlib-version>
    <jsp-version>1.2</jsp-version>
    <short-name>original tags</short-name>

    <tag>
        <name>Calendar</name>
        <tag-class>com.techscore.struts.chapter20.exercise1.CalendarTag</tag-class>
        <body-content>empty</body-content>
        <attribute>
            <name>year</name>
            <required>true</required>
            <rtexprvalue>true</rtexprvalue>
        </attribute>
        <attribute>
            <name>month</name>
            <required>true</required>
            <rtexprvalue>true</rtexprvalue>
        </attribute>
    </tag>

</taglib>

▼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/chapter21/exercise1/CalendarPlus"
                forward="calendarPlus211" />

    </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="calendarPlus211"
                path="/WEB-INF/JSP/com/techscore/struts/chapter21/exercise1/calendarPlus.jsp"
                controllerClass="com.techscore.struts.chapter21.exercise1.MenuController">
	    <put name="content" value="/WEB-INF/JSP/com/techscore/struts/chapter21/exercise1/content.jsp"/>
    </definition>

</tiles-definitions>

▼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/chapter21/exercise1/CalendarPlus.do

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

CalendarTag.javaの参照(20章の実習課題1と同じ)


↑このページの先頭へ

こちらもチェック!

PR
  • XMLDB.jp