目次へ

解答例 - 実習課題2 - 15.Javascriptでの検証/検証ルールの追加

(実習課題2)

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

  • 偶数であることをチェックする検証ルールを作成し、int型のプロパティに対して適応すること。

解答例

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

.
└─WEB-INF              web.xml(13章 実習課題1と同じ),struts-config.xml,validation.xml,
    │                   orig-validator-rules.xml,
    │                   validator-rules.xml(Strutsで提供されているものそのまま)
    ├─classes
    │  └─com
    │      └─techscore
    │          └─struts       EncodingFilter.class(2章 実習課題2と同じ)
    │              │           MessageResources.properties
    │              ├─chapter13
    │              │  └─exercise2 RuleCheckForm.class(13章 実習課題2と同じ)
    │              └─chapter15
    │                  └─exercise2 ValidateEven.class
    ├─JSP
    │  └─com
    │      └─techscore
    │          └─struts
    │              └─chapter15
    │                  └─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,
  commons-validator.jar
package com.techscore.struts.chapter15.exercise2;

import java.io.Serializable;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.validator.Field;
import org.apache.commons.validator.GenericValidator;
import org.apache.commons.validator.ValidatorAction;
import org.apache.commons.validator.ValidatorUtil;
import org.apache.struts.action.ActionError;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.validator.Resources;

public class ValidateEven implements Serializable {
    public static void validateEven(Object bean, ValidatorAction action,
                                    Field field, ActionErrors errors,
                                    HttpServletRequest request) {
        String value = ValidatorUtil.getValueAsString(bean, field.getProperty());
        //空白なら何もしない
        if (!GenericValidator.isBlankOrNull(value)) {
            if (GenericValidator.isInt(value)) {
                int num = Integer.parseInt(value);
                if (num % 2 == 0) {
                    //値が数字でかつ偶数ならば何もしない
                    return;
                }
            }
            ActionError error = Resources.getActionError(request, action, field);
            errors.add(field.getKey(), error);
        }
    }
}
<!-- inputTest.jsp -->
<!-- TECHSCORE Java JakartaStruts 15章 実習課題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 15章 実習課題2</title>
<html:javascript formName="/com/techscore/struts/chapter15/exercise2/OutputTest" method="check" />
</head>
<body>

<h2>テストデータ入力</h2>
<p>どちらも数字を入力し、登録ボタンを押してください。</p>

<html:form action="/com/techscore/struts/chapter15/exercise2/OutputTest" 
onsubmit="return check(this)">

    <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>
<!-- 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-nested" prefix="nested" %>
<%@ taglib uri="http://jakarta.apache.org/struts/tags-html" prefix="html" %>

<html>
<head><title>TECHSCORE Java JakartaStruts 15章 実習課題2</title></head>
<body>

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

<table border="2">

    <tr><th>String</th>
    <td><nested:root name="ruleCheckForm132"><nested:write property="testString" /></td></tr>

    <tr><th>int</th>
    <td><nested:write property="testInt" /></nested:root></td></tr>

</table>

<p><html:link action="/com/techscore/struts/chapter15/exercise2/InputTest">入力に戻る</html:link></p>

</body>
</html>

▼MessageResources.properties

errors.header=<font color="red">
errors.footer=</font>
testString.notInteger=String must be integer
testInt.notInteger=int must be integer
testString.maxlengthError=String length must be equal to or less than {0}
testInt.minlengthError=int length must be equal to or more than {0}
testString.notEven=String must be even
testInt.notEven=int must be even
productCode.illegal=check productCode
amount.illegal=check amount
name.required=製品名は必須です。
name.invalidate=製品名が不正です。

▼orig-validator-rules.xml

<?xml version="1.0" encoding="Shift_JIS" ?>
<!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>
   <global>
      <validator name="even"
            classname="com.techscore.struts.chapter15.exercise2.ValidateEven"
               method="validateEven"
         methodParams="java.lang.Object,
                       org.apache.commons.validator.ValidatorAction,
                       org.apache.commons.validator.Field,
                       org.apache.struts.action.ActionErrors,
                       javax.servlet.http.HttpServletRequest"
                  msg="errors.invalidate">
      </validator>
   </global>
</form-validation>

▼struts-config.xml

<?xml version="1.0" encoding="ISO_8859-1"?>
<!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/chapter15/exercise2/InputTest"
            forward="/WEB-INF/JSP/com/techscore/struts/chapter15/exercise2/inputTest.jsp" />
    <action path="/com/techscore/struts/chapter15/exercise2/OutputTest"
            name="ruleCheckForm132"
            scope="request"
            validate="true"
            input="/WEB-INF/JSP/com/techscore/struts/chapter15/exercise2/inputTest.jsp"
            forward="/WEB-INF/JSP/com/techscore/struts/chapter15/exercise2/outputTest.jsp" />
  </action-mappings>
  
  <message-resources parameter="com.techscore.struts.Messages" />
  
  <plug-in className="org.apache.struts.validator.ValidatorPlugIn">
    <set-property property="pathnames"
                  value="/WEB-INF/validator-rules.xml,/WEB-INF/orig-validator-rules.xml,
                         /WEB-INF/validation.xml" />
  </plug-in>
</struts-config>

▼validation.xml

<?xml version="1.0" encoding="ISO_8859-1"?>
<!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/chapter15/exercise2/OutputTest">
            <field property="testString" depends="integer,even,maxlength">
                <msg name="integer" key="testString.notInteger" />
                <msg name="even" key="testString.notEven" />
                <msg name="maxlength" key="testString.maxlengthError" />
                <arg0 name="maxlength" key="${var:maxlength}" resource="false" />
                <var>
                    <var-name>maxlength</var-name>
                    <var-value>3</var-value>
                </var>
            </field>
            <field property="testInt" depends="integer,even,minlength">
                <msg name="integer" key="testInt.notInteger" />
                <msg name="even" key="testInt.notEven" />
                <msg name="minlength" key="testInt.minlengthError" />
                <arg0 name="minlength" key="${var:minlength}" resource="false" />
                <var>
                    <var-name>minlength</var-name>
                    <var-value>3</var-value>
                </var>
            </field>
        </form>
    </formset>
</form-validation>

▼起動URLは以下の通り

WEB_ROOT/com/techscore/struts/chapter15/exercise2/InputTest.do

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

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

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


↑このページの先頭へ

こちらもチェック!

PR
  • XMLDB.jp