解答例 - 実習課題1 - 16.DynaValidatorForm/DynaValidatorActionForm
(実習課題1)
15章実習課題1のWebアプリケーションを改良しなさい。
- DynaValidatorFormを使用するようにする事。
解答例
▼ディレクトリ構成は以下の通り
. └─WEB-INF web.xml(13章 実習課題1と同じ),struts-config.xml,validation.xml, │ validator-rules.xml(Strutsで提供されているものそのまま) ├─classes │ └─com │ └─techscore │ └─struts EncodingFilter.class(2章 実習課題2と同じ) │ MessageResources.properties(14章 実習課題2と同じ) ├─JSP │ └─com │ └─techscore │ └─struts │ └─chapter16 │ └─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, commons-validator.jar
<!-- inputTest.jsp --> <!-- TECHSCORE Java JakartaStruts 16章 実習課題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 16章 実習課題1</title> <html:javascript formName="ruleCheckForm161" method="check" /> </head> <body> <h2>テストデータ入力</h2> <p>どちらも数字を入力し、登録ボタンを押してください。</p> <html:form action="/com/techscore/struts/chapter16/exercise1/OutputTest" onsubmit="return check(this)"> <table> <tr><th><label for="testString">String</label></th> <td><html:text property="testString" styleId="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 16章 実習課題1</title></head> <body> <h2>テストデータ入力</h2> <p>登録されたデータは以下の通りです</p> <table border="2"> <tr><th>String</th> <td><nested:root name="ruleCheckForm161"><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/chapter16/exercise1/InputTest">入力に戻る</html:link></p> </body> </html>
▼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="ruleCheckForm161" type="org.apache.struts.validator.DynaValidatorForm"> <form-property name="testString" type="java.lang.String" initial="" /> <form-property name="testInt" type="int" initial="0" /> </form-bean> </form-beans> <action-mappings> <action path="/com/techscore/struts/chapter16/exercise1/InputTest" forward="/WEB-INF/JSP/com/techscore/struts/chapter16/exercise1/inputTest.jsp" /> <action path="/com/techscore/struts/chapter16/exercise1/OutputTest" name="ruleCheckForm161" scope="request" validate="true" input="/WEB-INF/JSP/com/techscore/struts/chapter16/exercise1/inputTest.jsp" forward="/WEB-INF/JSP/com/techscore/struts/chapter16/exercise1/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/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="ruleCheckForm161"> <field property="testString" depends="integer,maxlength"> <msg name="integer" key="testString.notInteger" /> <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,minlength"> <msg name="integer" key="testInt.notInteger" /> <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/chapter16/exercise1/InputTest.do
EncodingFilter.javaの参照(2章の実習課題2と同じ)
MessageResources.propertiesの参照(14章の実習課題2と同じ)