• Struts2 输入校验 第四弹


    ActionSupport 里面有一个validate.可以重写里面你的方法。

    校验执行流程:

    1)首先进行类型转化

    2)然后进行输入校验(执行validate方法)

    3)如果在上述过程中出现了任何错误(1 or 2),都不会再去执行execute方法。会转向struts.xm中该action的名为input的result所对应的页面。

    actionsupport类的addActionError()方法的实现:首先创建一个arrayList对象.然后将错误消息添加到该ArrayList对象中。 

    当调用getActionError()方法返回Action级别的错误信息列表时,返回的实际上是集合的一个副本而不是集合本身,因此对集合副本调用clear()方法清除的依旧是副本中的元素而非原集合中的元素,此时原集合中的内容没有受到任何影响,换句话话,Action级别的错误信息列表对开发者来说是只读的。 

    FieldError级别的错误信息底层是LinkedHashMap实现的,该Map的key是string类型,value是List<String>类型,这就表示一个field name可以对应多条错误信息,这些错误信息都放置在List<String>集合中。

    <%@ 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 'register.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>
        <h2><font color="blue">用户注册</font></h2>
        
        <s:actionerror cssStyle="color:red"/>
        
        ----------------------------------------
        
        <s:fielderror cssStyle="color:blue"></s:fielderror>
        
        <!-- 
        <form action="register.action">
        
        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>
        age: <input type="text" name="age" size="20"><br>
        birthday: <input type="text" name="birthday" size="20"><br>
        graduation: <input type="text" name="graduation" size="20"><br>
        
        <input type="submit" value="submit"/>
        
        </form>
         -->
        <s:form action="register.action" theme="simple">
        
        username: <s:textfield name="username" label="username"></s:textfield><br>
        password: <s:password name="password" label="password"></s:password><br>
        repassword: <s:password name="repassword" label="repassword"></s:password><br>
        age: <s:textfield name="age" label="age"></s:textfield><br>
        birthday: <s:textfield name="birthday" label="birthday"></s:textfield><br>
        graduation: <s:textfield name="graduation" label="graduation"></s:textfield><br>
        
        <s:submit value="submit"></s:submit>
           
        </s:form>
        
      </body>
    </html>
    register.jsp
     1 <?xml version="1.0" encoding="UTF-8"?>
     2 <!DOCTYPE struts PUBLIC
     3     "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
     4     "http://struts.apache.org/dtds/struts-2.0.dtd">
     5     
     6 <struts>
     7     
     8     <package name="struts2" extends="struts-default">
     9         <action name="register" class="com.shengsiyuan.struts2.RegisterAction">
    10             <result name="success">/registerResult.jsp</result>
    11             <result name="input">/register.jsp</result>
    12         </action>
    13     </package>
    14     
    15 </struts>
    struts.xml
    <?xml version="1.0" encoding="UTF-8"?>
    <web-app version="2.5" 
        xmlns="http://java.sun.com/xml/ns/javaee" 
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
        xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
        http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
      
      <filter>
          <filter-name>struts2</filter-name>
          <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
      </filter>
      
      <filter-mapping>
          <filter-name>struts2</filter-name>
          <url-pattern>/*</url-pattern>
      </filter-mapping>
      
    </web-app>
    web.xml
    import java.util.Calendar;
    import java.util.Date;
    
    import com.opensymphony.xwork2.ActionSupport;
    
    public class RegisterAction extends ActionSupport
    {
        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;
        }
    
        @Override
        public String execute() throws Exception
        {
            
            
            return SUCCESS;
        }
        
        @Override
        public void validate()
        {
            if(null == username || username.length() < 4 || username.length() > 6)
            {
                this.addActionError("username invalid");
                
                this.addFieldError("username", "username invalid in field");
            }
            
            if(null == password || password.length() < 4 || password.length() > 6)
            {
                this.addActionError("password invalid");
            }
            else if(null == repassword || repassword.length() < 4 || repassword.length() > 6)
            {
                this.addActionError("repassword invalid");
            }
            else if(!password.equals(repassword))
            {
                this.addActionError("the passwords not the same");
            }
            
            if(age < 10 || age > 50)
            {
                this.addActionError("age invalid");
            }
            
            if(null == birthday)
            {
                this.addActionError("birthday invalid");
            }
            
            if(null == graduation)
            {
                this.addActionError("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.addActionError("birthday should be before graduation");
                }
            }
            
            //this.getFieldErrors().clear();
            //this.getActionErrors().clear();
            
            this.clearActionErrors();
            this.clearFieldErrors();
            
            System.out.println("invoked!!!");
        }
        
    }
    RegisterAction
    <%@ 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 'registerResult.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>
        age:<s:property value="age"/><br>
        birthday:<s:property value="birthday"/><br>
        graduate:<s:property value="graduation"/>   
        
      </body>
    </html>
    registerResult.jsp
  • 相关阅读:
    乐观锁与悲观锁——解决并发问题
    CentOS7 loaded active exited
    ubuntu安装phpVirtualBox web服务
    linux drwxr-xr-x 是什么意思
    phpmyadmin配置文件权限错误,不应任何用户都能修改
    转: CentOS安装jdk8
    PostgreSQL windows service启动失败
    PostgreSQL 添加自定义变量
    数据库检查约束是否存在
    转:PostgreSQL Cheat Sheet
  • 原文地址:https://www.cnblogs.com/feiguo/p/8151346.html
Copyright © 2020-2023  润新知