入力値チェックがしたい
【やりたいこと】
名前と年齢を必須入力にする。
年齢に数値が入力されているかをチェックする。
チェックにひっかかったら、output.jspにはとばずに、
input.jspにとどまって、エラーメッセージを表示する。
【input.jspの編集】
input.jspにエラーメッセージを表示させるために、
表示させたい場所に<html:errors/>と追記する。
input.jsp(赤文字は追加部分)
<%@ page contentType="text/html; charset=Shift-JIS"%>
<%@ taglib uri="/struts-html.tld" prefix="html" %>
<html>
<head><title>入力画面</title></head>
<body>
名前・年齢をを入力し、血液型を選択してください。
<br>
<html:errors/>
<html:form action="/sendData" focus="name">
名前:
<html:text property="name" size="16"/>
<br>
年齢:
<html:text property="age" size="8" />
<br>
血液型:
<html:select property="bloodtype">
<html:option value="A">A</html:option>
<html:option value="B">B</html:option>
<html:option value="O">O</html:option>
<html:option value="AB">AB</html:option>
</html:select>
<br><br>
<html:submit property="submit" value="送信"/>
<br><br>
</html:form>
</body>
</html>
【TestActionForm.javaの編集】
継承をActionFormからValidatorFormにすることで、
入力チェックを有効にします。
TestActionForm.java(青文字は変更部分)
import org.apache.struts.validator.ValidatorForm;
public class TestActionForm extends ValidatorForm {
private String name;
private String age;
private String bloodtype;
public String getAge() {
return age;
}
public void setAge(String age) {
this.age = age;
}
public String getBloodtype() {
return bloodtype;
}
public void setBloodtype(String bloodtype) {
this.bloodtype = bloodtype;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
【struts-config.xmlの編集】
入力値チェックができるように、追記。
actionタグの「validate」は入力値チェックを行うかどうか、「input」はチェックに引っかかった場合にどのページに遷移するか。
plug-inタグはチェックをするために必要なプラグイン、設定ファイルについて記述。
struts-config.xml(赤文字は追記部分)
<?xml version="1.0" encoding="Shift_JIS"?>
<!DOCTYPE struts-config PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 1.1//EN"
"http://struts.apache.org/dtds/struts-config_1_1.dtd">
<struts-config>
<form-beans>
<form-bean name="form" type="TestActionForm" />
</form-beans>
<action-mappings>
<action path="/sendData" type="TestAction" name="form" validate="true" input="/jsp/input.jsp">
<forward name="success" path="/jsp/output.jsp" />
</action>
</action-mappings>
<message-resources parameter="ApplicationResources"/>
<plug-in className="org.apache.struts.validator.ValidatorPlugIn">
<set-property value="/WEB-INF/validator-rules.xml,/WEB-INF/validation.xml" property="pathnames" />
</plug-in>
</struts-config>
【validation.xmlの作成】
どんなチェックをするかをここに記述。
WEB-INFの下に作ってください。
同時に、WEB-INF/validator-rules.xmlが存在するか確認してください。
年齢のチェックでは、
- 年齢が入力されているか、数値かどうかをチェックします(fieldタグ)
- 入力されていないときは、(ApplicationResources.propertiesであとで記述する)errors.requiredを表示します。(msgタグ1)
- 数値出ないときは~.propertiesで記述したerrors.integerを表示します。(msgタグ2)
- そのときに引数で~.propertiesで記述したageを渡します。(arg0タグ)
validation.xml
<?xml version="1.0" encoding="Shift_JIS" ?>
<!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="form">
<field property="name" depends="required">
<msg name="required" key="errors.required" />
<arg0 key="name"/>
</field>
<field
property="age" depends="required,integer">
<msg name="required" key="errors.required" />
<msg name="integer" key="errors.integer" />
<arg0 key="age"/>
</field>
</form>
</formset>
</form-validation>
【ApplicationResources.propertiesの編集】
ここには表示するメッセージなどを記述します。
errors.*と記述されているものは、
input.jspで<html:errors/>と記述したところに表示されます。
- headarはエラーメッセージ出力前に1回出力される。
- footerはエラーメッセージが全て出力後に1回出力される。
- suffixは各エラーメッセージの最後に出力される
- {0}はvalidation.xmlのarg0タグに設定したものが出力される。
Application.properties
errors.header=<font color=red>
errors.footer=</font>
errors.suffix=<br>
name=氏名
age=年齢
errors.required={0}を入力してください。
errors.integer={0}には数値を入力してください。
合計: -
最終更新:2008年07月10日 13:33