目次へ

解答例 - 実習課題2 - 12.ValidatorForm/ValidatorActionForm

(実習課題2)

実習課題1のWebアプリケーションを、ValidatorActionFormを使用するように改良しなさい。

また同じActionクラスを別のpathに適応させ、異なる検証ルールをpath毎に設定できる事を確認する事。

解答例

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

.
└─WEB-INF              web.xml(実習課題1と同じ),struts-config.xml,validation.xml,
    │                   validator-rules.xml(Strutsで提供されているものそのまま)
    ├─classes
    │  └─com
    │      └─techscore
    │          └─struts       EncodingFilter.class(2章 実習課題2と同じ),
    │              │           MessageResources.properties(5章 実習課題2と同じ)
    │              └─chapter12
    │                  └─exercise2 InputEmployeeForm.class,OutputEmployeeAction.class
    ├─JSP
    │  └─com
    │      └─techscore
    │          └─struts
    │              └─chapter12
    │                  └─exercise2 inputEmployee.jsp,inputEmployee2.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
/**
 * InputEmployeeForm.java
 * TECHSCORE Java JakartaStruts 12章 実習課題2
 *
 * Copyright (c) 2004 Four-Dimensional Data, Inc.
 *
 */

package com.techscore.struts.chapter12.exercise2;

import org.apache.struts.validator.ValidatorActionForm;
import org.apache.struts.action.ActionMapping;
import javax.servlet.http.HttpServletRequest;

public class InputEmployeeForm extends ValidatorActionForm{
    private String name = "";
    private String[] address = {"", ""};
    //※配列のインデックスを指定する場合には宣言時に配列の確保が必要
    private int number = 0;
    private boolean gender = false;
    private boolean existAddress2 = false;
    private int telephone = 0;
  
    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 setTelephone(int telephone){
        this.telephone = telephone;
    }
  
    public int getTelephone(){
        return telephone;
    }

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

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

package com.techscore.struts.chapter12.exercise2;

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 12章 実習課題2 -->
<!-- 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 12章 実習課題2</title></head>
<body>

<h2>社員データ入力<font color="red">(社員番号を入力チェック)</font></h2>
<p>社員データを入力し、登録ボタンを押してください。</p>

<html:form action="/com/techscore/struts/chapter12/exercise2/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[0]" /></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ありにチェックをつけていないと、住所2を入力していても登録されません。<br>
※社員番号は数字2桁以上で入力してください。<br>

</body>
</html>
<!-- inputEmployee2.jsp -->
<!-- TECHSCORE Java JakartaStruts 12章 実習課題2 -->
<!-- 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 12章 実習課題2</title></head>
<body>

<h2>社員データ入力<font color="red">(名前を入力チェック)</font></h2>
<p>社員データを入力し、登録ボタンを押してください。</p>

<html:form action="/com/techscore/struts/chapter12/exercise2/OutputEmployee2.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[0]" /></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>


※名前は必ず入力してください。</br>
※住所2は複数登録が必要な場合のみチェックをつけて入力してください。</br>
 住所2ありにチェックをつけていないと、住所2を入力していても登録されません。</br>

</body>
</html>
<!-- outputEmployee.jsp -->
<!-- TECHSCORE Java JakartaStruts 12章 実習課題2 -->
<!-- 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 12章 実習課題2</title></head>
<body>

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

<jsp:useBean id="inputEmployeeForm122"
             class="com.techscore.struts.chapter12.exercise2.InputEmployeeForm"
             scope="request" />

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

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

    <% if (inputEmployeeForm122.getExistAddress2()) { %>
           <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 (inputEmployeeForm122.getGender()) { %>
           <tr><th>性別</th><td>男</td></tr>
    <% } else { %>
           <tr><th>性別</th><td>女</td></tr>
    <% } %>
</table>

<p><html:link action="/com/techscore/struts/chapter12/exercise2/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="inputEmployeeForm122"
                   type="com.techscore.struts.chapter12.exercise2.InputEmployeeForm" />
    </form-beans>

    <action-mappings>
        <action path="/com/techscore/struts/chapter12/exercise2/InputEmployee"
                forward="/WEB-INF/JSP/com/techscore/struts/chapter12/exercise2/inputEmployee.jsp" />
        <action path="/com/techscore/struts/chapter12/exercise2/OutputEmployee"
                type="com.techscore.struts.chapter12.exercise2.OutputEmployeeAction"
                name="inputEmployeeForm122"
                scope="request"
                validate="true"
                input="/WEB-INF/JSP/com/techscore/struts/chapter12/exercise2/inputEmployee.jsp">
            <forward name="output"
                     path="/WEB-INF/JSP/com/techscore/struts/chapter12/exercise2/outputEmployee.jsp" />
        </action>
        <action path="/com/techscore/struts/chapter12/exercise2/InputEmployee2"
                forward="/WEB-INF/JSP/com/techscore/struts/chapter12/exercise2/inputEmployee2.jsp" />
        <action path="/com/techscore/struts/chapter12/exercise2/OutputEmployee2"
                type="com.techscore.struts.chapter12.exercise2.OutputEmployeeAction"
                name="inputEmployeeForm122"
                scope="request"
                validate="true"
                input="/WEB-INF/JSP/com/techscore/struts/chapter12/exercise2/inputEmployee2.jsp">
            <forward name="output"
                     path="/WEB-INF/JSP/com/techscore/struts/chapter12/exercise2/outputEmployee.jsp" />
        </action>
    </action-mappings>

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

    <plug-in className="org.apache.struts.validator.ValidatorPlugIn">
        <set-property property="pathnames"
                      value="/WEB-INF/validator-rules.xml, /WEB-INF/validation.xml" />
    </plug-in>

</struts-config>

▼validation.xml

<?xml version="1.0" encoding="Windows-31J" ?>
<!DOCTYPE form-validation PUBLIC
  "-//Apache Software Foundation//DTD Commons Validator Rules Configuration 1.0//EN"
  "http://jakarta.apache.org/commons/dtds/validator_1_0.dtd">

<form-validation>

    <formset>
        <form name="/com/techscore/struts/chapter12/exercise2/OutputEmployee">
            <field property="number" depends="minlength">
                <msg name="minlength" key="invalid.number" />
                <var>
                    <var-name>minlength</var-name>
                    <var-value>2</var-value>
                </var>
            </field>
        </form>
        <form name="/com/techscore/struts/chapter12/exercise2/OutputEmployee2">
            <field property="name" depends="required">
                <msg name="required" key="invalid.name" />
            </field>
            <field property="number" depends="required">
                <msg name="required" key="invalid.number" />
            </field>
        </form>
    </formset>

</form-validation>

▼起動URLは以下の通り

WEB_ROOT/com/techscore/struts/chapter12/exercise2/InputEmployee.do
WEB_ROOT/com/techscore/struts/chapter12/exercise2/InputEmployee2.do
※2つめは同じActionクラスを別のpathに適応させたものです

web.xmlの参照(実習課題1と同じ)

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

MessageResources.propertiesの参照(5章の実習課題2と同じ)

MessageResource.txtの参照(5章の実習課題2と同じ)


↑このページの先頭へ

こちらもチェック!

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