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
<?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 输入校验 第四弹的更多相关文章
- Struts2输入校验
1.编写校验规则文件 (<ActionName>-validation.xml),文件放在Action类文件相同的路径下校验失败返回input的result. <vali ...
- Struts2 自定义输入校验 第五弹
Struts2的校验框架有两种:一种是validate方法,另一种是有效的xml文件. Action中自定义方法的输入校验,对于通过action的method属性所指定的自定义方法myExecute, ...
- 笔记:Struts2 输入校验
Struts2的输入校验包含了客户端校验和服务器端校验,通过编写校验规则文件来实现输入校验,需要增加 Convention 插件,将 struts2-convention-plugin-2.3.31. ...
- Struts2输入校验(XML方式)
本章主要介绍struts2的XML配置方式输入校验.以下将结合一个实例程序进行说明. 代码结构: 关键代码: RegistAction.javapackage com.alfred.regist.ac ...
- Struts2输入校验(编码方式)
struts2对用户输入数据的校验方法有两种方式,一种是通过编码的方式,另一种则是通过使用XML配置方式. 本章主要介绍struts2编码方式的输入校验.以下将结合一个实例程序进行说明. 代码结构: ...
- Struts2 输入校验(使用编码方式)
一.Struts输入校验 1.创建register.jsp <%@ page language="java" contentType="text/html; cha ...
- struts2对action中的方法进行输入校验(2)
struts2输入校验流程: 1.类型转换器对请求參数运行类型转换,并把转换后的值赋给aciton中的属性 2.假设在运行类型转换的过程中出现异常,系统会将异常信息保存到ActionContext, ...
- JavaWeb框架_Struts2_(六)----->Struts2的输入校验
1. 输入校验章节目录 输入校验概述 客户端校验 服务器端校验 手动编程校验 重写validate方法 重写validateXxx()方法 输入校验流程 校验框架校验 Struts2 内置的校验器 常 ...
- Struts 2(五):输入校验 & 校验框架
第一节 Struts2输入校验 1.1 输入校验的重要性 输入校验分为客户端校验和服务器端校验.客户端校验用来过滤用户的错误操作,一般使用JavaScript代码实现.服务器端校验用来防止非法用户的恶 ...
随机推荐
- 分层架构web容器的配置安全
转自:http://hi.baidu.com/shineo__o/item/7520d54c24d234c71081da82 /ps:本以为这是一个偶然配置失误造成的问题,但最近几天无聊时测试发现,有 ...
- 应用设置Setting的实现
有非常多应用都在iOS设置中有相关的设置.例如以下图: 通过这个设置能够方便的相应用的一些主要的设置进行更改. 要完整的实现这个设置功能,有下面几方面问题须要解决: 1)设置的编写(实现设置的 ...
- eclipse高速查找一个变量、方法或者类被引用的地方
近期不停debug,拿到一个变量之后总是要先概览一下才好下手,之前一直用Ctrl+F来做,太麻烦. 今天查了下eclipse使用,发现有快捷键,用法: 先双击要查看的变量.方法或者类,使之被选中,然后 ...
- 【BIEE】数据透视表格第一列添加序号
现在有这么一个需求,需要在数据透视图的表格前面条件一列序号,作为行号,如下图: 那么实现这个如何实现呢? 只需要在BIEE分析编辑界面,新建一列,然后公式定义为:RCOUNT(RSUM(1)) ,保存 ...
- Ant Design 3.0 使用案例
代码地址如下:http://www.demodashi.com/demo/12309.html 本文适合对象 有过React使用经验. 有过webpack使用经验. 了解node. DEMO使用方式 ...
- HttpClient 模拟登录搜狐微博
http://mengyang.iteye.com/blog/575671 第一次遇到一个这样的问题,"PKIX path building failed" 异常 详解异常: ...
- substring,subsequence,charAt执行效率的不同
package com.java.tencent; public class T_2_longestPalindrome { public String test1(String s){ long s ...
- 小东和三个朋友一起在楼上抛小球,他们站在楼房的不同层,假设小东站的楼层距离地面N米,球从他手里自由落下,每次落地后反跳回上次下落高度的一半,并以此类推知道全部落到地面不跳,求4个小球一共经过了多少米?(数字都为整数) 给定四个整数A,B,C,D,请返回所求结果。
include #include<vector> using namespace std; class Balls { public: int calcDistance(int A, in ...
- Spark源码分析之五:Task调度(一)
在前四篇博文中,我们分析了Job提交运行总流程的第一阶段Stage划分与提交,它又被细化为三个分阶段: 1.Job的调度模型与运行反馈: 2.Stage划分: 3.Stage提交:对应TaskSet的 ...
- openssl源码安装
下载最新的OpenSSL http://openssl.org/source/ ./config make make install 通过命令openssl version或者openssl ...