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

 <?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="struts2" extends="struts-default">
<action name="register" class="com.shengsiyuan.struts2.RegisterAction">
<result name="success">/registerResult.jsp</result>
<result name="input">/register.jsp</result>
</action>
</package> </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

Struts2 输入校验 第四弹的更多相关文章

  1. Struts2输入校验

    1.编写校验规则文件 (<ActionName>-validation.xml),文件放在Action类文件相同的路径下校验失败返回input的result.       <vali ...

  2. Struts2 自定义输入校验 第五弹

    Struts2的校验框架有两种:一种是validate方法,另一种是有效的xml文件. Action中自定义方法的输入校验,对于通过action的method属性所指定的自定义方法myExecute, ...

  3. 笔记:Struts2 输入校验

    Struts2的输入校验包含了客户端校验和服务器端校验,通过编写校验规则文件来实现输入校验,需要增加 Convention 插件,将 struts2-convention-plugin-2.3.31. ...

  4. Struts2输入校验(XML方式)

    本章主要介绍struts2的XML配置方式输入校验.以下将结合一个实例程序进行说明. 代码结构: 关键代码: RegistAction.javapackage com.alfred.regist.ac ...

  5. Struts2输入校验(编码方式)

    struts2对用户输入数据的校验方法有两种方式,一种是通过编码的方式,另一种则是通过使用XML配置方式. 本章主要介绍struts2编码方式的输入校验.以下将结合一个实例程序进行说明. 代码结构: ...

  6. Struts2 输入校验(使用编码方式)

    一.Struts输入校验 1.创建register.jsp <%@ page language="java" contentType="text/html; cha ...

  7. struts2对action中的方法进行输入校验(2)

    struts2输入校验流程: 1.类型转换器对请求參数运行类型转换,并把转换后的值赋给aciton中的属性 2.假设在运行类型转换的过程中出现异常,系统会将异常信息保存到ActionContext, ...

  8. JavaWeb框架_Struts2_(六)----->Struts2的输入校验

    1. 输入校验章节目录 输入校验概述 客户端校验 服务器端校验 手动编程校验 重写validate方法 重写validateXxx()方法 输入校验流程 校验框架校验 Struts2 内置的校验器 常 ...

  9. Struts 2(五):输入校验 & 校验框架

    第一节 Struts2输入校验 1.1 输入校验的重要性 输入校验分为客户端校验和服务器端校验.客户端校验用来过滤用户的错误操作,一般使用JavaScript代码实现.服务器端校验用来防止非法用户的恶 ...

随机推荐

  1. wordpress账户防暴力破解攻击

    一.修改数据库表前缀 默认的表前缀是wp_,如果你安装博客的时候没有修改,可以参考这篇文章修改下表前缀. 修改完登录测试下,如果登录成功后提示“您没有足够的权限访问该页面”,说明前缀没有修改完整,参照 ...

  2. Odoo11 重大改变

    Table of Contents 新特性 Activity 项目子任务 组织架构 地址 域 widget 功能重构 Quant 份 procurement 补货 自动动作 动作绑定 去掉了stock ...

  3. 【Python】Python中in与not in

    在python中,要判断特定的值是否存在列表中,可使用关键字in,判断特定的值不存在列表中,可使用关键字not in letters = ['A','B','C','D','E','F','G'] i ...

  4. nginx源代码分析--监听套接字的创建 套接字的监听 HTTP请求创建连接

    作为一个webserver,那么肯定是有监听套接字的,这个监听套接字是用于接收HTTP请求的,这个监听套接字的创建是依据配置文件的内容来创建的,在nginx.conf文件里有多少个地址就须要创建多少个 ...

  5. dede频道标签channel和频道内容标签channelartlist的调用栏目名的不同方式,如果错误使用标签会发生错误

    频道标签 [field:typename/] 频道内容标签 {dede:field name='typename'/}

  6. php 导出CSV抽象类

    php 导出CSV抽象类,依据总记录数与每批次记录数,计算总批次.循环导出.避免内存不足的问题. ExportCSV.class.php <? php /** php Export CSV ab ...

  7. Spring Security 表单登录

    1. 简介 本文将重点介绍使用Spring Security登录. 本文将构建在之前简单的Spring MVC示例之上,因为这是设置Web应用程序和登录机制的必不可少的. 2. Maven 依赖 要将 ...

  8. caffe-ubuntu1604-gtx850m-i7-4710hq----VGG_ILSVRC_16_layers.caffemodel

    c++调用vgg16: ./build/install/bin/classification \ /media/whale/wsWin10/wsCaffe/model-zoo/VGG16//deplo ...

  9. emacs 简记

    简介 Emacs作为神的编辑器,不用介绍了吧,说点感受. 用了一段时间了,总体感觉其实Emacs是很简单的,甚至比vim还简单,因为在X环境下,打开后可以就像记事本一样使用.但是,使用Emacs的人一 ...

  10. H2 应用实例2

    DIY 博客全文界面的推荐.反对.加关注.返回顶部.快速评论等小功能的集成 --> 转载 :一.搭建测试环境和项目 1.1.搭建JavaWeb测试项目 创建一个[H2DBTest]JavaWeb ...