<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    無線&移動互聯網技術研發

    換位思考·····
    posts - 19, comments - 53, trackbacks - 0, articles - 283
      BlogJava :: 首頁 :: 新隨筆 :: 聯系 :: 聚合  :: 管理

    Struts2 表單數據校驗

    Posted on 2010-03-30 00:43 Gavin.lee 閱讀(468) 評論(0)  編輯  收藏 所屬分類: SSH2 --Struts2

    http://m.tkk7.com/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");
                }

            }

            
        }

        
    }

    主站蜘蛛池模板: 2022国内精品免费福利视频 | 日韩成人精品日本亚洲| 亚洲一区二区三区精品视频| 亚洲精品国产情侣av在线| 777亚洲精品乱码久久久久久| 久久久无码精品亚洲日韩蜜桃| 国产亚洲成av人片在线观看| 亚洲精品乱码久久久久久中文字幕| 亚洲中文久久精品无码ww16| 亚洲精品乱码久久久久久按摩| 国产亚洲一区二区精品| 亚洲AV无码一区二区二三区入口| 亚洲国产精品久久久久| 亚洲精品国产成人中文| 亚洲日日做天天做日日谢| 精品亚洲成a人在线观看| 免费国产va视频永久在线观看| 一级毛片免费播放试看60分钟| 好猛好深好爽好硬免费视频| 东方aⅴ免费观看久久av| 88av免费观看| 成年女人毛片免费播放人| 国产小视频在线免费| 337p日本欧洲亚洲大胆裸体艺术| 亚洲av无码国产精品夜色午夜 | 免费看国产精品麻豆| 亚洲日韩国产精品乱| 亚洲成AV人片一区二区密柚| 1区1区3区4区产品亚洲| 中文字幕亚洲码在线| 免费视频精品一区二区| 久久精品免费一区二区三区| 99国产精品永久免费视频| 男女啪啪永久免费观看网站| 久久久亚洲精品蜜桃臀| 久久久久亚洲AV无码网站| 亚洲精品无码久久久久秋霞| 国产精品免费观看视频| 18pao国产成视频永久免费| 暖暖日本免费在线视频| 亚洲乱色熟女一区二区三区丝袜 |