---
title: "数据校验DataValiation"
author: "netxdiy"
date: "2011-03-20T14:46:33Z"
tags: ["Apache", "JSP", "Java", "Struts", "XML"]
categories: ["企业架构"]
slug: "datavaliation"
draft: false
lastmod: "2011-03-21T16:14:58Z"
id: 968224
url: "https://netxdiy.iteye.com/blog/968224"
---

struts.xml /result.jsp /register.jsp RegisterAction.java package com.demo.struts2.action; import java.util.Calendar; import java.util.Date; import com.opensymphony.xwork2.ActionSupport; public class RegisterAction extends ActionSupport{ private String username; private String password; private String repassword; private int age; private Date birthday; private Date graduate; @Override public String execute() throws Exception { // TODO Auto-generated method stub return SUCCESS; } @Override public void validate() { // TODO Auto-generated method stub if(username==null || username.length()&lt;4 || username.length()&gt;6) { this.addActionError("username invalid"); this.addFieldError("username","username invalid in field"); } if(password==null || password.length()&lt;4 || password.length()&gt;6) { this.addActionError("password invalid"); this.addFieldError("password","password invalid in field"); } else if(repassword==null || repassword.length()&lt;4 || repassword.length()&gt;6) { this.addActionError("repassword invalid"); this.addFieldError("repassword","password invalid in field"); } else if(!password.equals(repassword)) { this.addActionError("the passwords not the same"); } if(age&lt;10 || age&gt;50) { this.addActionError("age invalid"); } if(birthday==null) { this.addActionError("birthday invalid"); } if(graduate==null){ this.addActionError("graduate invalid"); } if(birthday!=null && graduate!=null) { Calendar c1=Calendar.getInstance(); c1.setTime(birthday); Calendar c2=Calendar.getInstance(); c2.setTime(graduate); if(!c1.before(c2)) { this.addActionError("birthday should be before graduate"); } } super.validate(); } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public String getRepassword() { return repassword; } public void setRepassword(String repassword) { this.repassword = repassword; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public Date getBirthday() { return birthday; } public void setBirthday(Date birthday) { this.birthday = birthday; } public Date getGraduate() { return graduate; } public void setGraduate(Date graduate) { this.graduate = graduate; } } register.jsp &lt;%@ page language="java" import="java.util.\*" pageEncoding="UTF-8"%&gt; &lt;%@taglib prefix="s" uri="/struts-tags" %&gt; &lt;% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %&gt;  My JSP 'register.jsp' starting page ------------------------------------------------------------------------- result.jsp &lt;%@ page language="java" import="java.util.\*" pageEncoding="UTF-8"%&gt; &lt;% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %&gt; &lt;%@taglib prefix="s" uri="/struts-tags" %&gt;  My JSP 'result.jsp' starting page username: password: repassword: age: birthday: graduate: