目次へ

解答例 - 実習課題1 - 11.DynaActionForm

(実習課題1)

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

  • DynaActionFormをそのまま使用すること。
  • パラメータのチェック機能は解除して良い。

解答例

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

.
└─WEB-INF              web.xml,struts-config.xml
    ├─classes
    │  └─com
    │      └─techscore
    │          └─struts       EncodingFilter.class(2章 実習課題2と同じ),
    │              │           MessageResources.properties(中身は空)
    │              └─chapter11
    │                  └─exercise1 OutputEmployeeAction.class
    ├─JSP
    │  └─com
    │      └─techscore
    │          └─struts
    │              └─chapter11
    │                  └─exercise1 inputEmployee.jsp,outputEmployee.jsp
    ├─lib              strutsライブラリjarファイル
    └─tld              struts-html.tld,struts-nested.tld

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

package com.techscore.struts.chapter11.exercise1;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class OutputEmployeeAction extends Action{

    public ActionForward execute(ActionMapping mapping, ActionForm form,
                               HttpServletRequest request, HttpServletResponse response)
            throws Exception{

        if (isCancelled(request)) {
            return mapping.getInputForward();
        }

        return mapping.findForward("output");

    }

}
<!-- inputEmployee.jsp -->
<!-- TECHSCORE Java JakartaStruts 11章 実習課題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 11章 実習課題1</title></head>
<body>

<h2>社員データ入力</h2>
<p>社員データを入力し、登録ボタンを押してください。</p>

<html:form action="/com/techscore/struts/chapter11/exercise1/OutputEmployee.do" method="post">

    <table>

        <tr><th>名前</th>
            <td><html:text property="name" /></td>
            <td><html:errors property="name" /></td></tr>

        <tr><th>住所</th>
            <td><html:text property="address[0]" /></td>
            <td><html:errors property="address" /></td></tr>

        <tr><th>住所2</th>
            <td><html:text property="address[1]" /></td>
            <td><html:errors property="address2" /></td></tr>

        <tr><th>社員番号</th>
            <td><html:text property="number" /></td>
            <td><html:errors property="number" /></td></tr>

        <tr><th>性別</th>
            <td><html:radio property="gender" value="true" />男
                <html:radio property="gender" value="false" />女</td>
            <td></td></tr>

    </table>

    <p><html:checkbox property="existAddress2" value="true" />住所2あり</p>

    <p><html:submit value="登録" />
       <html:cancel value="キャンセル" /></p>

</html:form>

※住所2は複数登録が必要な場合のみ入力してください。<br>
 また、住所2ありチェックがなければ登録されません。<br>
※社員番号は数字以外を入力すると0になります。<br>
※未入力チェックをしていないため、名前や住所が空でもそのまま登録されます。<br>

</body>
</html>
<!-- outputEmployee.jsp -->
<!-- TECHSCORE Java JakartaStruts 11章 実習課題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-nested"
           prefix="nested" %>

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

<h2>社員データ入力</h2>
<p>登録された社員データは以下の通りです</p>

<jsp:useBean id="inputEmployeeForm111"
             class="org.apache.struts.action.DynaActionForm"
             scope="request" />

<table border="2">
    <tr><th>氏名</th>
    <td><nested:root name="inputEmployeeForm111"><nested:write scope="request" property="name" /></td></tr>

    <tr><th>住所</th>
    <td><nested:write scope="request" property="address[0]" /></td></tr>

    <% if (((Boolean)inputEmployeeForm111.get("existAddress2")).booleanValue()) { %>
           <tr><th>住所2</th>
           <td><nested:write scope="request" property="address[1]" /></td></tr>
    <% } %>

    <tr><th>社員番号</th>
    <td><nested:write scope="request" property="number" /></nested:root></td></tr>

    <% if (((Boolean)inputEmployeeForm111.get("gender")).booleanValue()) { %>
           <tr><th>性別</th><td>男</td></tr>
    <% } else { %>
           <tr><th>性別</th><td>女</td></tr>
    <% } %>
</table>

<p><html:link action="/com/techscore/struts/chapter11/exercise1/InputEmployee">入力に戻る</html:link></p>

</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="inputEmployeeForm111"
                   type="org.apache.struts.action.DynaActionForm">
            <form-property name="name" type="java.lang.String" initial="" />
            <form-property name="address" type="java.lang.String[]" initial="'', ''" />
            <form-property name="number" type="int" initial="0" />
            <form-property name="gender" type="boolean" initial="false" />
            <form-property name="existAddress2" type="boolean" initial="false" />
        </form-bean>
    </form-beans>

    <action-mappings>
        <action path="/com/techscore/struts/chapter11/exercise1/InputEmployee"
                forward="/WEB-INF/JSP/com/techscore/struts/chapter11/exercise1/inputEmployee.jsp" />
        <action path="/com/techscore/struts/chapter11/exercise1/OutputEmployee"
                type="com.techscore.struts.chapter11.exercise1.OutputEmployeeAction"
                name="inputEmployeeForm111"
                scope="request"
                validate="true"
                input="/WEB-INF/JSP/com/techscore/struts/chapter11/exercise1/inputEmployee.jsp">
            <forward name="output"
                     path="/WEB-INF/JSP/com/techscore/struts/chapter11/exercise1/outputEmployee.jsp" />
        </action>
    </action-mappings>

    <message-resources parameter="com.techscore.struts.MessageResources" />

</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>
    <taglib>
        <taglib-uri>http://jakarta.apache.org/struts/tags-nested</taglib-uri>
        <taglib-location>/WEB-INF/tld/struts-nested.tld</taglib-location>
    </taglib>

</web-app>

▼起動URLは以下の通り

WEB_ROOT/com/techscore/struts/chapter11/exercise1/InputEmployee.do

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


↑このページの先頭へ

こちらもチェック!

PR
  • XMLDB.jp