解答例 - 実習課題1 - 13.validatorで提供されている検証ルール
(実習課題1)
以下のWebアプリケーションを作成しなさい。
- Stringとint型のプロパティを持つActionFormに、テキストフィールドから入力した値を設定する。
- ActionFormのプロパティに対して、integerルールで検証を行う。
- 検証をクリアした場合には、ActionFormの値を表示する。
- int型のプロパティにintegerルールを適応しても意味が無いことを確認すること。
解答例
▼ディレクトリ構成は以下の通り
.
└─WEB-INF web.xml,struts-config.xml,validation.xml,
│ validator-rules.xml(Strutsで提供されているものそのまま)
├─classes
│ └─com
│ └─techscore
│ └─struts EncodingFilter.class(2章 実習課題2と同じ),
│ │ MessageResources.properties
│ └─chapter13
│ └─exercise1 RuleCheckForm.class
├─JSP
│ └─com
│ └─techscore
│ └─struts
│ └─chapter13
│ └─exercise1 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章 実習課題1
*
* Copyright (c) 2004 Four-Dimensional Data, Inc.
*
*/
package com.techscore.struts.chapter13.exercise1;
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章 実習課題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-html"
prefix="html" %>
<html>
<head><title>TECHSCORE Java JakartaStruts 13章 実習課題1</title></head>
<body>
<h2>テストデータ入力</h2>
<p>どちらも数字を入力し、登録ボタンを押してください。</p>
<html:form action="/com/techscore/struts/chapter13/exercise1/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>
</body>
</html>
<!-- outputTest.jsp -->
<!-- TECHSCORE Java JakartaStruts 13章 実習課題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-html"
prefix="html" %>
<%@ taglib uri="http://jakarta.apache.org/struts/tags-nested"
prefix="nested" %>
<html>
<head><title>TECHSCORE Java JakartaStruts 13章 実習課題1</title></head>
<body>
<h2>テストデータ入力</h2>
<p>登録されたデータは以下の通りです</p>
<table border="2">
<tr><th>String</th>
<td><nested:root name="ruleCheckForm131"><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/exercise1/InputTest">入力に戻る</html:link></p>
</body>
</html>
▼MessageResources.properties
testString.notInteger=String not integer testInt.notInteger=int not integer
▼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="ruleCheckForm131"
type="com.techscore.struts.chapter13.exercise1.RuleCheckForm" />
</form-beans>
<action-mappings>
<action path="/com/techscore/struts/chapter13/exercise1/InputTest"
forward="/WEB-INF/JSP/com/techscore/struts/chapter13/exercise1/inputTest.jsp" />
<action path="/com/techscore/struts/chapter13/exercise1/OutputTest"
name="ruleCheckForm131"
scope="request"
validate="true"
input="/WEB-INF/JSP/com/techscore/struts/chapter13/exercise1/inputTest.jsp"
forward="/WEB-INF/JSP/com/techscore/struts/chapter13/exercise1/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/exercise1/OutputTest">
<field property="testString" depends="integer">
<msg name="integer" key="testString.notInteger" />
</field>
<field property="testInt" depends="integer">
<msg name="integer" key="testInt.notInteger" />
</field>
</form>
</formset>
</form-validation>
<?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>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Action</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
<taglib>
<taglib-uri>http://jakarta.apache.org/struts/tags-html</taglib-uri>
<taglib-location>/WEB-INF/tld/struts-html.tld</taglib-location>
</taglib>
<taglib>
<taglib-uri>http://jakarta.apache.org/struts/tags-nested</taglib-uri>
<taglib-location>/WEB-INF/tld/struts-nested.tld</taglib-location>
</taglib>
</web-app>
▼起動URLは以下の通り
WEB_ROOT/com/techscore/struts/chapter13/exercise1/InputTest.do
EncodingFilter.javaの参照(2章の実習課題2と同じ)

