zoukankan      html  css  js  c++  java
  • Struts2输入校验

    类型转换输入校验执行流程:

    1)首先执行类型转换

    2)然后执行输入校验

    3)如果以上任一项出现错误将不执行excute方法。直接跳转到本action对相应的名为input的result页面

    action:

    @Override
    	public void validate() {
    	//姓名校验
    		if (null==username||username.length()<3||username.length()>10){
    			this.addActionError(" username invalid");
    			this.addFieldError("username", "username invalid in field!");
    		}
    		//密码校验
    		if (null==password||password.length()<3||password.length()>6){
    			this.addActionError("password invalid!!");
    		}else if (repassword==null||repassword.length()<3||repassword.length()>6){
    			
    			this.addActionError("repassword invalid!!");	
    		}
    		else if (!password.equals(repassword)){
    			this.addActionError("The passwords not same!!");
    		}
    		//年龄校验
    		if(age<10||age>60){
    			this.addActionError("age invalid");
    			
    		}
    		//出生日期校验
    		if (null==birthday){
    			this.addActionError("birthday invalid!!");
    		}
    		//毕业日期校验
    		if(null==graduate){
    			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.after(c2)){
    				this.addActionError("Birthday not befor graduate!!");
    			}
    		}
    		

    struts标签库:

    <s:actionerror cssStyle="color=red"/>
      ``````````````````````````````````````
      <s:fielderror cssStyle="color=blue"></s:fielderror>

     <s:form action="register.action" theme="simple">
        	姓名:<s:textfield name="username" label="username"></s:textfield><br>
        	密码:<s:password name="password" label="password"></s:password><br>
        	重置密码:<s:password name="repassword" label="repassword"></s:password><br>
        	年龄:<s:textfield name="age" label="age"></s:textfield><br>
        	出生日期:<s:textfield name="birthday" label="birthday"></s:textfield><br>
        	毕业日期:<s:textfield name="graduate" label="graduate"></s:textfield><br>
        	<s:submit value="提交"></s:submit>
        
        </s:form>



     

  • 相关阅读:
    刷题-力扣-223. 矩形面积
    刷题-力扣-135. 分发糖果
    刷题-力扣-134. 加油站
    刷题-力扣-343. 整数拆分
    刷题-力扣-162. 寻找峰值
    刷题-力扣-371. 两整数之和
    20191114-2 Beta事后诸葛亮会议
    Beta阶段贡献分配
    扛把子组20191114-4 Beta发布用户使用报告
    beta 2/2 阶段中间产物提交
  • 原文地址:https://www.cnblogs.com/lixingle/p/3313002.html
Copyright © 2011-2022 走看看