ここまでのまとめ。
最終的なソース
LoginForm.java(WEB-INF/classes)
import org.apache.struts.validator.ValidatorForm; public class LoginForm extends ValidatorForm { private String userID; private String password; public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public String getUserID() { return userID; } public void setUserID(String userID) { this.userID = userID; } }
Login.jsp(/)
<%@page contentType="text/html; charset=Windows-31J"%> <%@taglib uri="/tags/struts-html" prefix="html"%> <html:html> <head> <meta HTTP-EQUIV="Content-Type" CONTENT="text/html;charset=Windows-31J"> <meta HTTP-EQUIV="Content-Language" CONTENT="ja"> <meta http-equiv="Pragma" content="no-cache"> <meta http-equiv="Cache-Control" content="no-cache"> </head> <body> <html:errors/> <html:form action="/Login"> 名前:<html:text property="userID"/><br> パスワード:<html:text property="password"/><br> <html:submit property="submit" value="ログイン"/> </html:form> </body> </html:html>
<?xml version="1.0" encoding="iso-8859-1"?> <!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.2//EN" "http://struts.apache.org/dtds/struts-config_1_2.dtd"> <struts-config> <data-sources /> <form-beans> <form-bean name="loginForm" type="LoginForm" /> </form-beans> <global-exceptions /> <global-forwards /> <action-mappings> <action path="/Login" type="LoginAction" name="loginForm" scope="request" input="/Login.jsp" validate="true"> <forward name="next" path="/loginSuccess.jsp"/> <forward name="reject" path="/rejectUser.jsp"/> </action> </action-mappings> <message-resources parameter="MessageResources" /> <plug-in className="org.apache.struts.validator.ValidatorPlugIn"> <set-property property="pathnames" value="/WEB-INF/validator-rules.xml,/WEB-INF/validation.xml" /> <set-property property="stopOnFirstError" value="true" /> </plug-in> </struts-config>
validation.xml(WEB-INF)
<?xml version="1.0" encoding="iso-8859-1"?> <!DOCTYPE form-validation PUBLIC "-//Apache Software Foundation//DTD Commons Validator Rules Configuration 1.1.3//EN" "http://jakarta.apache.org/commons/dtds/validator_1_1_3.dtd"> <form-validation> <formset> <form name="loginForm"> <field property="userID" depends="required,maxlength"> <arg0 key="loginForm.userID"/> <arg1 name="maxlength" key="${var:maxlength}" resource="false" /> <var> <var-name>maxlength</var-name> <var-value>5</var-value> </var> </field> <field property="password" depends="required,maxlength"> <arg0 key="loginForm.password"/> <arg1 name="maxlength" key="${var:maxlength}" resource="false" /> <var> <var-name>maxlength</var-name> <var-value>5</var-value> </var> </field> </form> </formset> </form-validation>
MessageResources.properties(WEB-INF/classes)
errors.required={0} is required. errors.minlength={0} can not be less than {1} characters. errors.maxlength={0} can not be greater than {1} characters. errors.invalid={0} is invalid. loginForm.userID=User ID loginForm.password=Password
MessageResources_ja.properties(WEB-INF/classes)
errors.required={0} \u3092\u5165\u529b\u3057\u3066\u304f\u3060\u3055\u3044\u3002 errors.minlength={0} \u306f {1} \u6587\u5b57\u4ee5\u4e0a\u3067\u306a\u3051\u308c\u3070\u3044\u3051\u307e\u305b\u3093 errors.maxlength={0} \u306f {1} \u6587\u5b57\u4ee5\u4e0b\u3067\u306a\u3051\u308c\u3070\u306a\u308a\u307e\u305b\u3093\u3002 errors.invalid={0} \u306f\u6b63\u3057\u304f\u3042\u308a\u307e\u305b\u3093\u3002 errors.range={0} \u306f {1} \u304b\u3089 {2} \u306e\u9593\u3067\u306a\u3051\u308c\u3070\u306a\u308a\u307e\u305b\u3093\u3002 loginForm.userID=\u30e6\u30fc\u30b6\u30fcID loginForm.password=\u30d1\u30b9\u30ef\u30fc\u30c9
validator-rules.xml(WEB-INF)
commons-validator.jar(WEB-INF/lib)
細かい設定とかは Struts のマニュアルでも、google 先生にでも問い合わせてみりゃ。
これでサーバーサイド検証機能はOK。
次はクライアントサイト検証(Javascript)と Dynaform / DynaValidatorForm のあたりかなー
しかし、そろそろ Eclipse でも導入しないとしんどい感じになってきた。Struts の内部へステップインすればログ読みながら1歩1歩って感じじゃ無い駆け足での進化が可能な筈やから。
どうすっかなー