无线&移动互联网技术研发

换位思考·····
posts - 19, comments - 53, trackbacks - 0, articles - 283
  BlogJava :: 首页 :: 新随笔 :: 联系 :: 聚合  :: 管理

Struts2 表单数据校验

Posted on 2010-03-30 00:43 Gavin.lee 阅读(452) 评论(0)  编辑  收藏 所属分类: SSH2 --Struts2

http://www.blogjava.net/max/archive/2006/11/14/81106.html
All Input Is Evil!

利用Action的validate方法进行的注册表单验证

一、注册输入页面 reg.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"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
    
<head>
        
<title>welcomme register page !</title>
    
</head>
    
<s:fielderror></s:fielderror>
    
<body bgcolor="red">
        
<s:form action="reg" method="post">
            
<table align="center" width="40%" border="1" bgcolor="cyan">
                
<tr>
                    
<td>username:</td>
                    
<td><input type="text" name="username" /></td>
                
</tr>
                
<tr>
                    
<td>password:</td>
                    
<td><input type="text" name="password" /></td>
                
</tr>
                
<tr>
                    
<td>re-password:</td>
                    
<td><input type="text" name="repassword" /></td>
                
</tr>
                
<tr>
                    
<td>age:</td>
                    
<td><input type="text" name="age" /></td>
                
</tr>
                
<tr>
                    
<td>
                        birthday:
                    
</td>
                    
<td>
                        
<input type="text" name="birthday" />
                    
</td>
                
</tr>
                
<tr>
                    
<td>graduation:</td>
                    
<td><input type="text" name="graduation" /></td>
                
</tr>
                
<tr>
                    
<td><input type="submit" value="Submit " /></td>
                    
<td><input type="reset" value="Reset " /></td>
                
</tr>
            
</table>
        
</s:form>
    
</body>
</html>


二、Struts2 核心配置文件中对RegAction的映射
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
    "http://struts.apache.org/dtds/struts-2.0.dtd"
>
<struts>
    
<package name="interceptor1" extends="struts-default">
        
<action name="reg" class="reg.RegAction">
            
<result name="success">/success.jsp</result>
            
<result name="input">/reg.jsp</result>
        
</action>
    
</package>
</struts>

三、执行验证
package reg;

import java.util.Calendar;
import java.util.Date;

import com.opensymphony.xwork2.ActionSupport;

public class RegAction extends ActionSupport {

    
/**
     * 
     
*/

    
private static final long serialVersionUID = 1L;

    
private String username;

    
private String password;

    
private String repassword;

    
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 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() throws Exception {
        
//DO Register Process
        return SUCCESS;
    }


    
// 验证form表单输入信息
    public void validate() {
        
this.clearErrorsAndMessages();
        
        
// 名字为空 或者长度小于6大于10
        if (null == username || username.length() < 6 || username.length() > 10{
            
this.addFieldError("username""username invalid");
        }

        
        
// 密码为空或者长度小于6或者大于10
        if (null == password || password.length() < 6 || password.length() > 10{
            
this.addFieldError("password""password invalid");
            
// 确认密码为空或者长度小于6或者大于10
        }
 else if (null == repassword || repassword.length() < 6 || repassword.length() > 10{
            
this.addFieldError("repassword""re-password invalid");
            
// 密码和确认密码值不是一样的
        }
 else if (!password.equals(repassword)) {
            
this.addFieldError("repassword""re-password and password not accord");
        }

        
        
// 年龄长度小于1或者大于150
        if (age < 1 || age > 150{
            
this.addFieldError("age""age invalid");
        }

        
        
// birthday 和 graduation 为空 或者 birthday 在 graduation 之前
        if (null == birthday) {
            
this.addFieldError("birthday""birthday invalid");
        }

        
        
if (null == graduation) {
            
this.addFieldError("graduation""graduation invalid");
        }

        
        
if (null != birthday && null != graduation) {
            Calendar c1 
= Calendar.getInstance();
            c1.setTime(birthday);

            Calendar c2 
= Calendar.getInstance();
            c2.setTime(graduation);

            
if (c1.before(c2)) {
                
this.addFieldError("graduation""birthday be forword graduation");
            }

        }

        
    }

    
}


只有注册用户登录后才能发表评论。


网站导航: