ITeye博客

数据校验DataValiation

netxdiy · 2011-03-20 14:46 · struts2 · 企业架构 · Java · Apache · Struts · XML · JSP · Markdown

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()<4 || username.length()>6) { this.addActionError("username invalid"); this.addFieldError("username","username invalid in field"); } if(password==null || password.length()<4 || password.length()>6) { this.addActionError("password invalid"); this.addFieldError("password","password invalid in field"); } else if(repassword==null || repassword.length()<4 || repassword.length()>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<10 || age>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 <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <%@taglib prefix="s" uri="/struts-tags" %> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> My JSP 'register.jsp' starting page ------------------------------------------------------------------------- result.jsp <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <%@taglib prefix="s" uri="/struts-tags" %> My JSP 'result.jsp' starting page username: password: repassword: age: birthday: graduate:

评论(0)

暂无评论。

评论功能维护中,当前仅展示历史评论。