目次へ

解答例 - 実習課題1 - 7.HTMLタグライブラリ3

(実習課題1)

以下のWebアプリケーションを、Strutsを用いて作成しなさい。

  • selectタグとoption/options/optionsCollectionタグを用いて、同じ内容の選択肢を表示する3つのフォームを作成する事。

解答例

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

.
├─com
│  └─techscore
│      └─struts
│          └─chapter7
│              └─exercise1     useSelectTag.jsp
└─WEB-INF              web.xml,struts-config.xml
    ├─classes
    │  └─com
    │      └─techscore
    │          └─struts       EncodingFilter.class(2章 実習課題2と同じ),
    │              └─chapter7
    │                  └─exercise1 SelectTagForm.class,Days.class
    ├─lib              strutsライブラリjarファイル
    └─tld              struts-html.tld

※strutsライブラリjarファイル
  struts.jar,commons-beanutils.jar,commons-collections.jar,commons-digester.jar,commons-logging.jar
/**
 * Days.java
 * TECHSCORE Java JakartaStruts 7章 実習課題1
 *
 * Copyright (c) 2004 Four-Dimensional Data, Inc.
 *
 */

package com.techscore.struts.chapter7.exercise1;

public class Days {

    public String[] getValues(){
        String[] labels={"1", "2", "3", "4", "5", "6", "7"};
        return labels;
    }

    public String[] getLabels(){
        String[] values={"日曜日", "月曜日", "火曜日", "水曜日", "木曜日", "金曜日", "土曜日"};
        return values;
    }

}
/**
 * SelectTagForm.java
 * TECHSCORE Java JakartaStruts 7章 実習課題1
 *
 * Copyright (c) 2004 Four-Dimensional Data, Inc.
 *
 */

package com.techscore.struts.chapter7.exercise1;

import org.apache.struts.action.ActionForm;
import org.apache.struts.util.LabelValueBean;

import java.util.List;
import java.util.LinkedList;

public class SelectTagForm extends ActionForm{

    private String[] daySelect1;
    private String[] daySelect2;
    private String[] daySelect3;
    private List days2 = new LinkedList();

    public SelectTagForm() {
        days2.add(new LabelValueBean("日曜日","1"));
        days2.add(new LabelValueBean("月曜日","2"));
        days2.add(new LabelValueBean("火曜日","3"));
        days2.add(new LabelValueBean("水曜日","4"));
        days2.add(new LabelValueBean("木曜日","5"));
        days2.add(new LabelValueBean("金曜日","6"));
        days2.add(new LabelValueBean("土曜日","7"));
    }

    public void setDaySelect1(String[] daySelect1){
        this.daySelect1 = daySelect1;
    }

    public String[] getDaySelect1(){
        return daySelect1;
    }

    public void setDaySelect2(String[] daySelect2){
        this.daySelect2 = daySelect2;
    }

    public String[] getDaySelect2(){
        return daySelect2;
    }

    public void setDaySelect3(String[] daySelect3){
        this.daySelect3 = daySelect3;
    }

    public String[] getDaySelect3(){
        return daySelect3;
    }

    public void setDays2(List days2) {
        this.days2 = days2;
    }

    public List getDays2(){
        return days2;

    }

}
<!-- useSelectTag.jsp -->
<!-- TECHSCORE Java JakartaStruts 7章 実習課題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" %>

<html>
<head><title>TECHSCORE Java JakartaStruts 7章 実習課題1</title></head>
<body>


<h3>Optionタグで表示</h3>

<p>都合のよい曜日を選択してください。</p>

<html:form action="/com/techscore/struts/chapter7/exercise1/SelectTag.do" method="post" >
    <html:select property="daySelect1" size="7">
        <html:option value="1">日曜日</html:option>
        <html:option value="2">月曜日</html:option>
        <html:option value="3">火曜日</html:option>
        <html:option value="4">水曜日</html:option>
        <html:option value="5">木曜日</html:option>
        <html:option value="6">金曜日</html:option>
        <html:option value="7">土曜日</html:option>
    </html:select>
</html:form>


<h3>Optionsタグで表示</h3>

<p>都合のよい曜日を選択してください。</p>

<%  pageContext.setAttribute("days",new com.techscore.struts.chapter7.exercise1.Days()); %>
<html:form action="/com/techscore/struts/chapter7/exercise1/SelectTag.do" method="post" >
    <html:select property="daySelect2" size="7">
        <html:options name="days" property="values"
                      labelName="days" labelProperty="labels" />
    </html:select>
</html:form>


<h3>OptionsSelectionタグで表示</h3>

<p>都合のよい曜日を選択してください。</p>

<html:form action="/com/techscore/struts/chapter7/exercise1/SelectTag.do" method="post" >
    <html:select property="daySelect3" size="7">
        <html:optionsCollection property="days2" value="value" label="label" />
    </html:select>
</html:form>


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

    <form-beans>
        <form-bean name="selectTagForm"
                   type="com.techscore.struts.chapter7.exercise1.SelectTagForm" />
    </form-beans>

    <action-mappings>
        <action path="/com/techscore/struts/chapter7/exercise1/SelectTag"
                type="com.techscore.struts.chapter7.exercise1.SelectTagAction"
                name="selectTagForm"
                scope="request">
        </action>
    </action-mappings>

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

<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-html</taglib-uri>
        <taglib-location>/WEB-INF/tld/struts-html.tld</taglib-location>
    </taglib>

</web-app>

▼起動URLは以下の通り

WEB_ROOT/com/techscore/struts/chapter7/exercise1/useSelectTag.jsp

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


↑このページの先頭へ

こちらもチェック!

PR
  • XMLDB.jp