博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Struts2数据校验
阅读量:7110 次
发布时间:2019-06-28

本文共 4279 字,大约阅读时间需要 14 分钟。

hot3.png

回到之前的LoginAction,我们只是简单的回显了数据,但我们的操作都是假设数据是正确的。但是假设我们在输入age的时候输入了字符串,服务器就会throw异常,而且age也无法接收到正确的值。

所以我们需要对数据进行校验。

 

Struts2中的数据不合法分两种情况:

一是Field级别的错误,例如给age输入字符串,还有Date不按格式输入

二是Action级别的逻辑错误,例如username不能过长,两次password必须相同等。

下面以一个注册RegisterAction说明Struts2如何进行数据校验。

 

我们先编写jsp页面。

register.jsp的内容如下:

 

<%@ 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+"/";%>              用户注册            

用户注册

----------------------------------------
----------------------------------------
username:
password:
repassword:
age:
birthday:
graduation:

register_result.jsp内容如下:

<%@ 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+"/";%>              用户注册结果            username: 
password:
age:
birthday:
graduate:

 

然后我们编写核心的RegisterAction,

package com.test.action;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;    //get 和 set方法        @Override    public String execute() throws Exception    {        return SUCCESS;    }}

 

这跟以前我们编写的步骤完全相同,但是这里我们需要进行数据校验。所以我们添加一个validate方法,这个也是对基类的重写。

@Override    public void validate()    {        if (username == null || username.length() < 4 || username.length() > 8)        {            this.addActionError("username invalid!.");        }        if (password == null || password.length() < 4 || password.length() > 8)        {            this.addActionError("password invalid!!");        }        else if (!password.equals(repassword))        {            this.addActionError("the passwords not the same");        }        if (!birthday.before(graduation))        {            this.addActionError("birthday must be earlier than graduation!!");        }                if(birthday != null && graduation != null && !birthday.before(graduation))        {            this.addActionError("birthday must be earlier than graduation!!");        }    }

我们可以看上,上面就是对username等进行逻辑上的校验。

然后我们修改struts.xml,可以去查看结果。

 

还有一种校验,是采用配置文件,我们将上面的validate注释掉,然后在action包下面,新建文件RegisterAction-validation.xml文件,内容为:

username can't be blank!
4
8
password can't be blank!
4
8
length of password should be between ${minLength} and ${maxLength}
age can't be blank!
10
40
age should be between ${min} and ${max}
birthday can't be blank!
2005-1-1
2007-12-31
birthday should be between ${min} and ${max}

起到的作用是相同的,当然,这种方式相对于第一种比较死板。

 

上面的校验都是Action级别的错误。如果是Field级别的错误,系统默认信息是XXX invalid,我们可以配置信息,显示更具体一些:

在action下新建文件RegisterAction.properties。内容如下:

invalid.fieldvalue.age \u5E74\u9F84\u5FC5\u987B\u4E3A\u6574\u6570invalid.fieldvalue.birthday=\u751F\u65E5\u4E0D\u5408\u6CD5invalid.fieldvalue.graduation=\u6BD5\u4E1A\u65E5\u671F\u4E0D\u5408\u6CD5
这里的信息是中文。此时我们在age中输入字符串时,显示的就是中文错误提示。

转载于:https://my.oschina.net/inevermore/blog/388690

你可能感兴趣的文章
centos6.5编译调试mysql-5.7.18
查看>>
第七周作业
查看>>
JQuery实现简单实用的气泡提示插件
查看>>
IDEA注册
查看>>
第四次实验
查看>>
12. MySQL简单使用
查看>>
GIS基础知识
查看>>
【java并发编程艺术学习】(五)第二章 java并发机制的底层实现原理 学习记录(三) 原子操作的实现原理学习...
查看>>
使用npm安装一些包失败了的看过来(npm国内镜像介绍)
查看>>
Goland中Redis的set求并集的错误处理
查看>>
Timer
查看>>
ComboBox
查看>>
C++ sort()函数和C qsort()函数用法总结
查看>>
【图像处理】工业相机原理详述 (转载)
查看>>
【分布式】Zookeeper应用场景
查看>>
【堆】
查看>>
Asp.net基础概念整理(一) Web应用程序和网站的区别
查看>>
[02-02 ]Java数据库链接范列
查看>>
一些常用的Bootstrap模板资源站
查看>>
taro 填坑之路(二)taro 通过事件监听 实现组件间传值
查看>>