解答例 - 実習課題3 - 13.validatorで提供されている検証ルール
(実習課題3)
実習課題2のWebアプリケーションと同じものを、maskルールを用いて作成しなさい。
解答例
▼ディレクトリ構成は以下の通り
.
└─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
│ └─exercise3 RuleCheckForm.class
├─JSP
│ └─com
│ └─techscore
│ └─struts
│ └─chapter13
│ └─exercise3 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章 実習課題3
*
* Copyright (c) 2004 Four-Dimensional Data, Inc.
*
*/
package com.techscore.struts.chapter13.exercise3;
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章 実習課題3 -->
<!-- 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章 実習課題3</title></head>
<body>
<h2>テストデータ入力</h2>
<p>どちらも数字を入力し、登録ボタンを押してください。</p>
<html:form action="/com/techscore/struts/chapter13/exercise3/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章 実習課題3 -->
<!-- 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章 実習課題3</title></head>
<body>
<h2>テストデータ入力</h2>
<p>登録されたデータは以下の通りです</p>
<table border="2">
<tr><th>String</th>
<td><nested:root name="ruleCheckForm133"><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/exercise3/InputTest">入力に戻る</html:link></p>
</body>
</html>
▼MessageResources.properties
testString.maskError=String mask error testInt.maskError=int mask 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="ruleCheckForm133"
type="com.techscore.struts.chapter13.exercise3.RuleCheckForm" />
</form-beans>
<action-mappings>
<action path="/com/techscore/struts/chapter13/exercise3/InputTest"
forward="/WEB-INF/JSP/com/techscore/struts/chapter13/exercise3/inputTest.jsp" />
<action path="/com/techscore/struts/chapter13/exercise3/OutputTest"
name="ruleCheckForm133"
scope="request"
validate="true"
input="/WEB-INF/JSP/com/techscore/struts/chapter13/exercise3/inputTest.jsp"
forward="/WEB-INF/JSP/com/techscore/struts/chapter13/exercise3/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/exercise3/OutputTest">
<field property="testString" depends="mask">
<msg name="mask" key="testString.maskError" />
<var>
<var-name>mask</var-name>
<var-value>^[0-9]{1,3}$</var-value>
</var>
</field>
<field property="testInt" depends="mask">
<msg name="mask" key="testInt.maskError" />
<var>
<var-name>mask</var-name>
<var-value>^[0-9]{3,}$</var-value>
</var>
</field>
</form>
</formset>
</form-validation>
▼起動URLは以下の通り
WEB_ROOT/com/techscore/struts/chapter13/exercise3/InputTest.do
EncodingFilter.javaの参照(2章の実習課題2と同じ)

