码迷,mamicode.com
首页 > 其他好文 > 详细

Struts2 validate校验

时间:2016-03-11 22:08:30      阅读:190      评论:0      收藏:0      [点我收藏+]

标签:

一般的,用户注册的时候,我们需要校验一些用户提交过来的参数。

一般有两道屏障,一是在前台页面上使用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>
View Code

struts.xml:

技术分享
        <action name="regist" class="com.shensiyuan.struts.action.RegistAction">
           <result name="success">/registResult.jsp</result>
           <result name="input">/regist.jsp</result>
        </action>
View Code

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!");
       }
    }

}
View Code

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>
View Code

一组效果图:

技术分享技术分享

 

Struts2 validate校验

标签:

原文地址:http://www.cnblogs.com/aigeileshei/p/5267139.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!