1、以前是在动作类的excute业务方法里写校验代码,这样是不好的,校验代码和业务逻辑代码混合,可读性差,而且增加维护难度。
2、我们现在使用validate或validateXxx方法校验。就是在动作类里重写ActionSupport类的validate()或放到自定义的validateXxx(),但是这样当字段较多的时候,也会很臃肿。
3、所以最好的办法就是定义一个动作类名-validation.xml或动作类名-动作名-validation.xml,并且保存在动作类同一目录下的xml校验配置文件。如动作类名为BAction,则该xml为BAction-validation.xml.
然后必须在xml校验配置文件中对每个校验字段配置校验器。
校验器的配置方法有两种
字段型校验方式
非字段型方式
一般用第一种,其中的key是引用国际化。
例子:
User.java
public class User {
private String name;
private String password1;
private String password2;
private int age;
private Date birthday;
private String email;
//get和set方法省略
}
注册动作类,注意一般要把pojo和动作类分开写,
不要动作类里还写上面那些字段。但是表单提交是提交到动作类的,也就是说动作类必须得有相同和字段取获取表单提交的值。这时候我们用到ModelDriver,可以看看这个http://blog.csdn.net/li_tengfei/article/details/6098145
···
package com.ljs.struts.action;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
public class RegisterAction extends ActionSupport implements ModelDriven {
private User user = new User();
public Object getModel() {
// TODO Auto-generated method stub
return user;
}
public String register() throws Exception {
return SUCCESS;
}
}
register.jsp
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<link rel="stylesheet" type="text/css" href="<s:url value="/styles/styles.css"/>">
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
</head>
<body>
<s:form action="Register" namespace="/user">
<s:textfield name="name" label="用户名"/>
<s:password name="password1" label="密码"/>
<s:password name="password2" label="确认密码"/>
<s:textfield name="age" label="年龄"/>
<s:textfield name="birthday" label="生日"/>
<s:textfield name="email" label="email"/>
<s:submit value="提交" cssStyle="float:right"/>
</s:form>
</body>
</html>
style.css
设置label为斜体,出错就为红色和斜体,错误信息红色和加粗
.label {font-style:italic; }
.errorLabel {font-style:italic; color:red; }
.errorMessage {font-weight:bold; color:red; }
RegisterAction-Register.xml
记住Register的R要大写,虽然方法是小写,但是这里要大写,没有会出错。
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE validators PUBLIC
"-//Apache Struts//XWork Validator 1.0.2//EN"
"http://struts.apache.org/dtds/xwork-validator-1.0.2.dtd">
<validators>
<field name="name">
<field-validator type="requiredstring">
<message key="errors.name.requiredstring"/>
</field-validator>
<field-validator type="stringlength">
<param name="minlength">2</param>
<param name="maxlength">6</param>
<message key="errors.namelength"/>
</field-validator>
</field>
<field name="password1">
<field-validator type="requiredstring">
<message key="errors.password1.requiredstring"/>
</field-validator>
<field-validator type="regex">
<param name="regexExpression">
<![CDATA[(\w{4,15})]]>
</param>
<message key="errors.password"/>
</field-validator>
</field>
<field name="password2">
<field-validator type="fieldexpression">
<param name="expression">
<![CDATA[password1==password2]]>
</param>
<message key="errors.passwordequals"/>
</field-validator>
</field>
<field name="age">
<field-validator type="int">
<param name="min">1</param>
<param name="max">150</param>
<message key="errors.age"/>
</field-validator>
</field>
<field name="birthday">
<field-validator type="required">
<message key="birthday.error.message"/>
</field-validator>
<field-validator type="date">
<param name="min">1990-01-01</param>
<param name="max">2017-01-01</param>
<message key="birthday.error.message"/>
</field-validator>
</field>
<field name="email">
<field-validator type="email">
<message key="errors.email"/>
</field-validator>
</field>
</validators>
RegisterAction.properties
struts.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.5//EN"
"http://struts.apache.org/dtds/struts-2.5.dtd">
<!-- Struts2配置文件的根元素 -->
<struts>
<package name="lab07" extends="struts-default" namespace="/user">
<action name="Register" class="com.ljs.struts.action.RegisterAction" method="register">
<result name="input">/register.jsp</result>
<result name="success">/success.jsp</result>
</action>
</package>
</struts>