在struts2中配置验证框架时,回报上面的错误。这时候,要学会读懂英文意思。文件不能被验证的XML定义。下面看完整的错误提示。
The file cannot be validated as the XML definition "http://www.opensymphony.com/xwork/xwork-validator-1.0.2.dtd" that is specified as describing the syntax
of the file cannot be located.
马上到xwork核心文件中找xwork-validator-1.0.2.dtd的文件的约定规则和自己写的意义对应。
<validators>
<field name="user.name">
<field-validator type="requiredstring">
<param name="trim">true</param>
<message>用户名不能为空</message>
</field-validator>
<field-validator type="stringlength">
<param name="maxLength">10</param>
<param name="minLength">6</param>
<message>用户名长度必须在${minLength}和${maxLength}之间</message>
</field-validator>
</field>
</validators>
<validators>
<field name="user.pwd">
<field-validator type="requiredstring">
<param name="trim">true</param>
<message>密码不能为空</message>
</field-validator>
<field-validator type="stringlength">
<param name="minLength">6</param>
<message>长度必须大于${minLength}</message>
</field-validator>
</field>
<field name="repwd">
<field-validator type="requiredstring">
<param name="trim">true</param>
<message>确认密码不能为空</message>
</field-validator>
<field-validator type="fieldexpression">
<param name="expression">user.pwd==repwd</param>
<message>密码和确认密码必须相等</message>
</field-validator>
</field>
</validators>
对比发现 是根节点,我多写了。只保留一个就好了。
完整jsp页面代码:
<s:fielderror/>
<s:form action="reg" method="post">
<s:textfield name="user.name" label="用户名" />
<s:password name="user.pwd" label="密码" />
<s:password name="repwd" label="确认密码" />
<s:textfield name="user.telephone" label="电话" />
<s:textfield name="user.username" label="用户姓名" />
<s:submit value="立即注册" />
</s:form>
配置文件,配置文件要用请求文件放在一起。命名要注意,如果action命名:RegisterAction,配置文件为:RegisterAction-validation.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE validators PUBLIC
"-//OpenSymphony Group//XWork Validator 1.0.2//EN"
"http://www.opensymphony.com/xwork/xwork-validator-1.0.2.dtd" >
<validators>
<field name="user.name">
<field-validator type="requiredstring">
<param name="trim">true</param>
<message>用户名不能为空</message>
</field-validator>
<field-validator type="stringlength">
<param name="maxLength">10</param>
<param name="minLength">6</param>
<message>用户名长度必须在${minLength}和${maxLength}之间</message>
</field-validator>
</field>
<field name="user.pwd">
<field-validator type="requiredstring">
<param name="trim">true</param>
<message>密码不能为空</message>
</field-validator>
<field-validator type="stringlength">
<param name="minLength">6</param>
<message>长度必须大于${minLength}</message>
</field-validator>
</field>
<field name="repwd">
<field-validator type="requiredstring">
<param name="trim">true</param>
<message>确认密码不能为空</message>
</field-validator>
<field-validator type="fieldexpression">
<param name="expression">user.pwd==repwd</param>
<message>密码和确认密码必须相等</message>
</field-validator>
</field>
<field name="user.telephone">
<field-validator type="requiredstring">
<param name="trim">true</param>
<message>电话号码不能为空</message>
</field-validator>
<field-validator type="regex">
<param name=" regex ">^(\d{3,4}-){0,1}(\d{7,8}))$</param>
<message>电话格式不正确</message>
</field-validator>
</field>
<field name="user.username">
<field-validator type="requiredstring">
<param name="trim">true</param>
<message>用户姓名不能为空</message>
</field-validator>
<field-validator type="stringlength">
<param name="maxLength">10</param>
<param name="minLength">2</param>
<message>用户名长度必须在${minLength}和${maxLength}之间</message>
</field-validator>
</field>
</validators>
acrion
package actions;
import com.opensymphony.xwork2.ActionSupport;
import entity.User;
public class RegisterAction extends ActionSupport {
private User user;
private String repwd;
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
public String getRepwd() {
return repwd;
}
public void setRepwd(String repwd) {
this.repwd = repwd;
}
public String execute(){
return SUCCESS;
}
}