java struts页面验证设置
1、在struts-config.xml中添加如下代码
<plug-in className="org.apache.struts.validator.ValidatorPlugIn">
<set-property property="pathnames" value="/org/apache/struts/validator/validator-rules.xml,/WEB-INF/validations.xml"/>
</plug-in>
<set-property property="pathnames" value="/org/apache/struts/validator/validator-rules.xml,/WEB-INF/validations.xml"/>
</plug-in>
2、在WEB-INF下建立validations.xml,假设验证的form名为inputForm,验证name,title,content不能为空,代码如下
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE form-validation PUBLIC
"-//Apache Software Foundation//DTD Commons Validator Rules Configuration 1.3.0//EN"
"http://jakarta.apache.org/commons/dtds/validator_1_3_0.dtd">
<form-validation>
<formset>
<form name="inputForm">
<field property="name" depends="required">
<msg name="required" key="guest.inputForm.name"/>
</field>
<field property="title" depends="required">
<msg name="required" key="guest.inputForm.title"/>
</field>
<field property="content" depends="required">
<msg name="required" key="guest.inputForm.content"/>
</field>
</form>
</formset>
</form-validation>
<!DOCTYPE form-validation PUBLIC
"-//Apache Software Foundation//DTD Commons Validator Rules Configuration 1.3.0//EN"
"http://jakarta.apache.org/commons/dtds/validator_1_3_0.dtd">
<form-validation>
<formset>
<form name="inputForm">
<field property="name" depends="required">
<msg name="required" key="guest.inputForm.name"/>
</field>
<field property="title" depends="required">
<msg name="required" key="guest.inputForm.title"/>
</field>
<field property="content" depends="required">
<msg name="required" key="guest.inputForm.content"/>
</field>
</form>
</formset>
</form-validation>
3、设置显示信息的资源文件
guest.inputForm.content = content is null
guest.inputForm.name = name is null
guest.inputForm.title = title is null
4、在jsp文件的<body>前面添加<html:javascript formName="inputForm"/>,并修改<html:form action="/input" >,改为<html:form action="/input" onsubmit="return validateInputForm(this)">