解答例 - 実習課題2 - 13.validatorで提供されている検証ルール
(実習課題2)
実習課題1のWebアプリケーションを改良しなさい。
- 2つのプロパティに対して、minlength, maxlengthルールの片方だけを適応すること。
解答例
▼ディレクトリ構成は以下の通り
.
└─WEB-INF              web.xml(実習課題1と同じ),struts-config.xml,validation.xml,
    │                   validator-rules.xml(Strutsで提供されているものそのまま)
    ├─classes
    │  └─com
    │      └─techscore
    │          └─struts       EncodingFilter.class(2章 実習課題2と同じ),
    │              │           MessageResources.properties
    │              └─chapter13
    │                  └─exercise2 RuleCheckForm.class
    ├─JSP
    │  └─com
    │      └─techscore
    │          └─struts
    │              └─chapter13
    │                  └─exercise2 inputTest.jsp,outputTest.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
	/**
 * RuleCheckForm.java
 * TECHSCORE Java JakartaStruts 13章 実習課題2
 *
 * Copyright (c) 2004 Four-Dimensional Data, Inc.
 *
 */
package com.techscore.struts.chapter13.exercise2;
import org.apache.struts.validator.ValidatorActionForm;
public class RuleCheckForm extends ValidatorActionForm{
    private String testString = "";
    private int testInt = 0;
    public void setTestString(String testString){
        this.testString = testString;
    }
  
    public String getTestString(){
        return testString;
    }
    public void setTestInt(int testInt){
        this.testInt = testInt;
    }
  
    public int getTestInt(){
        return testInt;
    }
}
	<!-- inputTest.jsp -->
<!-- TECHSCORE Java JakartaStruts 13章 実習課題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 13章 実習課題2</title></head>
<body>
<h2>テストデータ入力</h2>
<p>どちらも数字を入力し、登録ボタンを押してください。</p>
<html:form action="/com/techscore/struts/chapter13/exercise2/OutputTest.do" method="post">
    <table>
        <tr><th>String</th>
            <td><html:text property="testString" /></td>
            <td><html:errors property="testString" /></td></tr>
        <tr><th>int</th>
            <td><html:text property="testInt" /></td>
            <td><html:errors property="testInt" /></td></tr>
    </table>
    <p><html:submit value="登録" /></p>
</html:form>
<p>※Stringは3文字以内、intは3桁以上のみ登録可能です。</p>
</body>
</html>
	<!-- outputTest.jsp -->
<!-- TECHSCORE Java JakartaStruts 13章 実習課題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 13章 実習課題2</title></head>
<body>
<h2>テストデータ入力</h2>
<p>登録されたデータは以下の通りです</p>
<table border="2">
    <tr><th>String</th>
    <td><nested:root name="ruleCheckForm132"><nested:write scope="request" property="testString" /></td></tr>
    <tr><th>int</th>
    <td><nested:write scope="request" property="testInt" /></nested:root></td></tr>
</table>
<p><html:link action="/com/techscore/struts/chapter13/exercise2/InputTest">入力に戻る</html:link></p>
</body>
</html>
	testString.notInteger=String not integer testInt.notInteger=int not integer testString.maxlengthError=String maxlength error testInt.minlengthError=int minlength error
▼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="ruleCheckForm132"
                   type="com.techscore.struts.chapter13.exercise2.RuleCheckForm" />
    </form-beans>
    <action-mappings>
        <action path="/com/techscore/struts/chapter13/exercise2/InputTest"
                forward="/WEB-INF/JSP/com/techscore/struts/chapter13/exercise2/inputTest.jsp" />
        <action path="/com/techscore/struts/chapter13/exercise2/OutputTest"
                name="ruleCheckForm132"
                scope="request"
                validate="true"
                input="/WEB-INF/JSP/com/techscore/struts/chapter13/exercise2/inputTest.jsp"
                forward="/WEB-INF/JSP/com/techscore/struts/chapter13/exercise2/outputTest.jsp" />
    </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/chapter13/exercise2/OutputTest">
            <field property="testString" depends="integer,maxlength">
                <msg name="integer" key="testString.notInteger" />
                <msg name="maxlength" key="testString.maxlengthError" />
                <var>
                    <var-name>maxlength</var-name>
                    <var-value>3</var-value>
                </var>
            </field>
            <field property="testInt" depends="integer,minlength">
                <msg name="integer" key="testInt.notInteger" />
                <msg name="minlength" key="testInt.minlengthError" />
                <var>
                    <var-name>minlength</var-name>
                    <var-value>3</var-value>
                </var>
            </field>
        </form>
    </formset>
</form-validation>
	▼起動URLは以下の通り
WEB_ROOT/com/techscore/struts/chapter13/exercise2/InputTest.do
EncodingFilter.javaの参照(2章の実習課題2と同じ)

![Webアプリ開発エンジニアのための技術情報サイト[テックスコア]](/common/img/description.gif) 
 







