一般的,用户注册的时候,我们需要校验一些用户提交过来的参数。
一般有两道屏障,一是在前台页面上使用js进行验证,直接杜绝了不正常信息的提交。二是将提交过来的信息进行验证,不通过则返回注册页面并显示错误信息,我们这里介绍的就是在action中使用validate方法实现数据校验。
action中是继承自ActionSupport类,ActionSupport实现了Validate接口,有一个空的validate方法。
在action中只需要重写一下validate方法就好了。运行程序的时候,会先执行validate方法然后执行execute()方法。
在validate方法中,有这三种增加ActionError的方法,一般我们使用第一中和第三种,一旦action中存在error,就表示校验未通过,action自动返回INPUT,可以在INPUT定义的result页面中接受错误提示。
regist.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+"/"; %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base href="<%=basePath%>"> <title>My JSP ‘regist.jsp‘ starting page</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> <!-- <link rel="stylesheet" type="text/css" href="styles.css"> --> </head> <body> <h1><font color="blue">注册信息</font></h1> <s:actionerror cssStyle="color:red;"/> <form action="regist.action" method="post"> username:<input type="text" name="username" size="20"/><br/> password:<input type="password" name="password" size="20"/><br/> repassword:<input type="password" name="repassword" size="20"/><br/> sex:<input type="text" name="sex" size="20"/><br/> age:<input type="text" name="age" size="20"/><br/> birthday:<input type="text" name="birthday" size="text"/><br/> graduation:<input type="text" name="graduation" size="20"/><br/> <input type="submit" value="submit"/> </form> </body> </html>
struts.xml:
<action name="regist" class="com.shensiyuan.struts.action.RegistAction">
<result name="success">/registResult.jsp</result>
<result name="input">/regist.jsp</result>
</action>
RegistAction.java:
package com.shensiyuan.struts.action; import java.util.Calendar; import java.util.Date; import java.util.regex.Matcher; import java.util.regex.Pattern; import com.opensymphony.xwork2.ActionSupport; public class RegistAction extends ActionSupport { private String username; private String password; private String repassword; private String sex; private int age; private Date birthday; private Date graduation; 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 String getSex() { return sex; } public void setSex(String sex) { this.sex = sex; } 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 getGraduation() { return graduation; } public void setGraduation(Date graduation) { this.graduation = graduation; } public String execute(){ return SUCCESS; } @Override public void validate(){ /** * 字符串使用正则验证,3步: * 1、将string变为正则对象 Pattern p1 = Pattern.compile(reg1); * 2、Matcher m = p1.matcher(username); * 3、 boolean b = m.matches(); * 4这等价于前面3步: boolean b = Pattern.matches("a*b", "aaaaab"); */ String reg1="^[0-9a-zA-Z]{4,8}$"; String reg2="^(wo)?man$"; // String reg3="^([01]\\d\\d\\d\\|[2][0]\\d\\d)-([0][1-9]|[1][0-2])\\-([0-2][0-9]|[3][01])$"; 使用下面的方法是用正则,不需要将正则的String字符串前后加// //System.out.println(username); boolean usn=Pattern.matches(reg1, username); boolean ps=Pattern.matches(reg1, password); boolean reps=password.equals(repassword); boolean sx=Pattern.matches(reg2, sex); boolean ag=(age>0&&age<=100); boolean tm=false; if(birthday!=null&&graduation!=null){ Calendar bir=Calendar.getInstance(); bir.setTime(birthday); Calendar gra=Calendar.getInstance(); gra.setTime(graduation); tm=(bir.before(gra)); } if(!usn){ this.addActionError("username should size in four to eight!"); } if(!ps){ this.addActionError("password should size in four to eight!"); } if(!reps){ this.addActionError("repassword should be same as password!"); } if(!sx){ this.addActionError("sex should be man or woman"); } if(!ag){ this.addActionError("age should between zero and hundred!"); } if(!tm){ this.addActionError("if exist,birthday shoule before graduation!"); } } }
registResult.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+"/"; %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base href="<%=basePath%>"> <title>My JSP ‘registResult.jsp‘ starting page</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> <!-- <link rel="stylesheet" type="text/css" href="styles.css"> --> </head> <body> username:<s:property value="username"/><br/> password:<s:property value="password"/><br/> repassword:<s:property value="repassword"/><br/> sex:<s:property value="sex"/><br/> age:<s:property value="age"/><br/> birthday:<s:property value="birthday"/><br/> graduation:<s:property value="graduation"/><br/> </body> </html>
一组效果图:
原文:http://www.cnblogs.com/aigeileshei/p/5267139.html