目次へ

解答例 - 実習課題1 - 5.パラメータ値のチェックとHTMLタグライブラリ

(実習課題1)

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

  • 名前・社員番号および少なくとも住所1つが入力されなかった場合には、再度入力ページを表示すること。

解答例

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

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

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

package com.techscore.struts.chapter5.exercise1;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionError;
import org.apache.struts.action.ActionErrors;
import javax.servlet.http.HttpServletRequest;

public class InputEmployeeForm extends ActionForm{
    private String name;
    private String[] address = {"", ""};
    //※配列のインデックスを指定する場合には宣言時に配列の確保が必要
    private int number;
    private boolean gender;
    private boolean existAddress2;
  
    public void setName(String name){
        this.name = name;
    }
  
    public String getName(){
        return name;
    }

    public void setAddress(String address[]){
        this.address = address;
    }
  
    public String[] getAddress(){
        return address;
    }

    public void setNumber(int number){
        this.number = number;
    }
  
    public int getNumber(){
        return number;
    }

    public void setGender(boolean gender){
        this.gender = gender;
    }
  
    public boolean getGender(){
        return gender;
    }

    public void setExistAddress2(boolean existAddress2){
        this.existAddress2 = existAddress2;
    }
  
    public boolean getExistAddress2(){
        return existAddress2;
    }

//以下のメソッドがない場合にはチェックボックスの値が初期化されない
    public void reset(ActionMapping mapping, HttpServletRequest request){
        existAddress2 = false;
    }

    public ActionErrors validate(ActionMapping mapping, HttpServletRequest request){

        ActionErrors errors = new ActionErrors();

        if (name.equals("")) {
            errors.add("name", new ActionError("invalid.name"));
        }
        if (address[0].equals("")) {
            errors.add("address", new ActionError("invalid.address"));
        }
        if ((existAddress2) && (address[1].equals(""))) {
            errors.add("address2", new ActionError("invalid.address2"));
        }
        if (number == 0) {
            errors.add("number", new ActionError("invalid.number"));
        }

        return errors;

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

package com.techscore.struts.chapter5.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{

        return mapping.findForward("output");

    }

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

<%@ page contentType="text/html; charset=Windows-31J"
         session="false" 
         pageEncoding="Windows-31J" %>

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

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

<form action="<%=request.getContextPath() %>/com/techscore/struts/chapter5/exercise1/InputEmployee.do" method="post">
    <table>
        <tr><th>名前</th><td><input type="text" name="name"></td></tr>
        <tr><th>住所</th><td><input type="text" name="address"></td></tr>
        <tr><th>住所2</th><td><input type="text" name="address"></td></tr>
        <tr><th>社員番号</th><td><input type="text" name="number"></td></tr>
        <tr><th>性別</th>
            <td><input type="radio" name="gender" value="true" CHECKED>男
                <input type="radio" name="gender" value="false">女</td></tr>
    </table>
    <p><input type="checkbox" name="existAddress2" value="true">住所2あり</p>
    <p><input type="submit" value="登録"></p>
</form>

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

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

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

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

<jsp:useBean id="inputEmployeeForm51"
             class="com.techscore.struts.chapter5.exercise1.InputEmployeeForm"
             scope="request" />

<table border="2">
    <tr><th>氏名</th>
    <td><bean:write name="inputEmployeeForm51" scope="request" property="name" /></td></tr>

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

    <% if (inputEmployeeForm51.getExistAddress2()) { %>
           <tr><th>住所2</th>
           <td><bean:write name="inputEmployeeForm51" scope="request" property="address[1]" /></td></tr>
    <% } %>

    <tr><th>社員番号</th>
    <td><bean:write name="inputEmployeeForm51" scope="request" property="number" /></td></tr>

    <% if (inputEmployeeForm51.getGender()) { %>
           <tr><th>性別</th><td>男</td></tr>
    <% } else { %>
           <tr><th>性別</th><td>女</td></tr>
    <% } %>
</table>

<p><a href="<%=request.getContextPath() %>/com/techscore/struts/chapter5/exercise1/inputEmployee.jsp">入力に戻る</p>

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

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

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

<jsp:useBean id="inputEmployeeForm51"
             class="com.techscore.struts.chapter5.exercise1.InputEmployeeForm"
             scope="request" />

<table border="2">
    <tr><th>氏名</th>
    <td><bean:write name="inputEmployeeForm51" scope="request" property="name" /></td></tr>

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

    <% if (inputEmployeeForm51.getExistAddress2()) { %>
           <tr><th>住所2</th>
           <td><bean:write name="inputEmployeeForm51" scope="request" property="address[1]" /></td></tr>
    <% } %>

    <tr><th>社員番号</th>
    <td><bean:write name="inputEmployeeForm51" scope="request" property="number" /></td></tr>

    <% if (inputEmployeeForm51.getGender()) { %>
           <tr><th>性別</th><td>男</td></tr>
    <% } else { %>
           <tr><th>性別</th><td>女</td></tr>
    <% } %>
</table>

<p><a href="<%=request.getContextPath() %>/com/techscore/struts/chapter5/exercise1/inputEmployee.jsp">入力に戻る</p>

</body></html>

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

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

</web-app>

▼起動URLは以下の通り

WEB_ROOT/com/techscore/struts/chapter5/exercise1/inputEmployee.jsp

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


↑このページの先頭へ

こちらもチェック!

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