本文分三个部分:
一.输入校验简介
二.为什么需要输入校验
三.输入校验的方式
四.输入校验实例
一 .输入校验简介:几乎所有的JAVA开发者都会遇到输入校验的问题,输入校验简单的说就是对前台页面文本框中输入的值进行判断,比如用户名、密码不能为空,二次输入密码不正确,输入长度不合格,日期不正确等的验证。比如
reg.jsp 界面:
如果直接点注册,而不输入任何数据的话,则出现下面的错误提示。
如果输入的数据不合法,则出现如下错误提示:
通过上面的两个截图,详细读者已经大概明白的输入校验的作用了。
输入校验的原因:
二.为什么需要输入校验:输入校验直接影响了系统的稳定性。因为不能保证每个用户都按照开发者的意图来输入数据,而往往有些用户喜欢恶意的输入些非法数据来进行测试和破坏,如上图中所示,生日这样的字段,格式虽然正确,但是在逻辑上缺是错误的!所以必须对用户输入的信息进行校验。良好的输入校验,是一个项目的必不可少的环节!
三.输入校验的方式:
1.JavaScript完成客户端校验
2.validate(),validateXxx()方法进行输入校验
3.基于框架的输入校验
4.AJAX输入校验
四.输入校验实例
本文写的是 基于框架的输入校验:
①业务控制器Action:UserReg.action
- package com.yaxing.register;
- import java.util.Date;
- import com.opensymphony.xwork2.ActionSupport;
- public class UserReg extends ActionSupport{
- private String username;
- private String password1;
- private String password2;
- private Date birthday;
- public String getUsername() {
- return username;
- }
- public void setUsername(String username) {
- this.username = username;
- }
- public String getPassword1() {
- return password1;
- }
- public void setPassword1(String password1) {
- this.password1 = password1;
- }
- public String getPassword2() {
- return password2;
- }
- public void setPassword2(String password2) {
- this.password2 = password2;
- }
- public Date getBirthday() {
- return birthday;
- }
- public void setBirthday(Date birthday) {
- this.birthday = birthday;
- }
- public String execute() throws Exception{
- return SUCCESS;
- }
- }
这就是一个普通JAVA类(POJO),execute()方法只是简单的返回一个SUCCESS 字符串。
为了使用Struts2的框架校验规则文件进行输入校验,需要建立一个特定的校验规则文件。此文件是一个xml格式的配置文件。该文件的命名规则为
Action名称-validation.xml 保存在Action实现类相同的目录下面。 本文的校验文件就是UserReg-validation.xml
如图所示
校验规则 这个使用的是
②字段校验UserReg-validation.xml
非字段校验在后面会有介绍
- <?xml version="1.0" encoding="UTF-8"?>
- <!DOCTYPE validators PUBLIC
- "-//OpenSymphony Group//XWork Validator Config 1.0//EN"
- "http://www.opensymphony.com/xwork/xwork-validator-1.0.2.dtd">
- <validators>
- <!-- username字段校验 -->
- <field name="username">
- <field-validator type="stringlength">
- <param name="minLength">4</param>
- <param name="maxLength">10</param>
- <message>用户名长度为${minLength}到${maxLength}之间!</message>
- </field-validator>
- <field-validator type="requiredstring">
- <message >用户名不能为空!</message>
- </field-validator>
- </field>
- <!-- password1字段校验 -->
- <field name="password1">
- <field-validator type="requiredstring">
- <param name="trim">true</param>
- <message>错误,密码不能为空!</message>
- </field-validator>
- <field-validator type="stringlength">
- <param name="minLength">4</param>
- <param name="maxLength">20</param>
- <message>用户密码长度为${minLength}到${maxLength}之间!</message>
- </field-validator>
- </field>
- <!-- password2字段校验 -->
- <field name="password2">
- <field-validator type="requiredstring">
- <param name="trim">true</param>
- <message>错误,确认密码不能为空!</message>
- </field-validator>
- <field-validator type="stringlength">
- <param name="minLength">4</param>
- <param name="maxLength">20</param>
- <message>用户密码长度为${minLength}到${maxLength}之间!</message>
- </field-validator>
- <field-validator type="fieldexpression">
- <param name="expression">password1==password2</param>
- <message>两次输入密码不一样!</message>
- </field-validator>
- </field>
- <!-- birthday字段校验 -->
- <field name="birthday">
- <field-validator type="date">
- <param name="min">1990-01-01</param>
- <param name="max">2011-04-14</param>
- <message>错误:生日应该在一个范围内!</message>
- </field-validator>
- </field>
- </validators>
③JSP视图:reg.jsp
- <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
- <%@ taglib prefix="s" uri="/struts-tags" %>
- <%@ taglib uri="/struts-dojo-tags" prefix="sx"%>
- <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
- <html>
- <head>
- <title>My JSP 'reg.jsp' starting page</title>
- <s:head/>
- <sx:head/>
- </head>
- <body>
- <s:form action="UserReg">
- <s:textfield name="username" label="用户名"></s:textfield>
- <s:password name="password1" label="密码"></s:password>
- <s:password name="password2" label="确认密码"></s:password>
- <sx:datetimepicker name="birthday" label="生日:" displayFormat="yyyy-MM-dd"></sx:datetimepicker>
- <s:submit value="注册"/>
- <s:reset value="重置"></s:reset>
- </s:form>
- </body>
- </html>
④reg_success.jsp成功页面
- <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
- <%@ taglib uri="/struts-tags" prefix="s" %>
- <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
- <html>
- <head>
- <title>注册成功</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>
- 欢迎<s:property value="username"/>,注册成功!
- <h1><s:text name="username" ></s:text></h1>
- <h1><s:text name="password1"></s:text></h1>
- <h1><s:text name="password2"></s:text></h1>
- <h1><s:text name="birthday"></s:text></h1>
- </body>
- </html>
⑤struts.xml 配置文件:
- <?xml version="1.0" encoding="UTF-8" ?>
- <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd">
- <struts>
- <package name="test" extends="struts-default">
- <action name="UserReg" class="com.yaxing.register.UserReg" method="execute">
- <result name="success">/reg_success.jsp</result>
- <result name="input">/reg.jsp</result>
- </action>
- </package>
- </struts>
说下字段校验和非字段校验的区别:作用一样,配置风格不一样而已。
字段校验文件格式:
- <validators>
- <field name="校验字段名称">
- <field-validator type="校验类型">
- <param name="参数1">参数值1</param>
- <param name="参数2">参数值2</param>
- ..........
- <message>校验失败后的错误信息</message>
- </field-validator>
- <field-validator type="校验类型">
- ........
- </field-validator>
- </field>
- </validators>
非字段校验文件:
- <validators>
- <validator type="校验类型">
- <param name="fieldName">字段名称</param>
- <param name="参数1">参数1</param>
- <param name="参数2">参数2</param>
- .............
- <message >校验异常信息message>
- </validator>
- ............
- </validators>
非字段校验配置:UserReg-validation.xml
- <?xml version="1.0" encoding="UTF-8"?>
- <!DOCTYPE validators PUBLIC
- "-//OpenSymphony Group//XWork Validator Config 1.0//EN"
- "http://www.opensymphony.com/xwork/xwork-validator-1.0.2.dtd">
- <validators>
- <!-- username字段校验 -->
- <validator type="stringlength">
- <param name="fieldName">username</param>
- <param name="minLength">4</param>
- <param name="maxLength">10</param>
- <message>用户名长度为${minLength}到${maxLength}之间!</message>
- </validator>
- <validator type="requiredstring">
- <param name="fieldName">username</param>
- <message >用户名不能为空!</message>
- </validator>
- <!-- password1字段校验 -->
- <validator type="requiredstring">
- <param name="fieldName">password1</param>
- <message>错误,密码不能为空!</message>
- </validator>
- <validator type="stringlength">
- <param name="fieldName">password1</param>
- <param name="minLength">4</param>
- <param name="maxLength">20</param>
- <message>用户密码长度为${minLength}到${maxLength}之间!</message>
- </validator>
- <!-- password2字段校验 -->
- <validator type="requiredstring">
- <param name="fieldName">password2</param>
- <param name="trim">true</param>
- <message>错误,确认密码不能为空!</message>
- </validator>
- <validator type="stringlength">
- <param name="fieldName">password2</param>
- <param name="minLength">4</param>
- <param name="maxLength">20</param>
- <message>用户密码长度为${minLength}到${maxLength}之间!</message>
- </validator>
- <validator type="fieldexpression">
- <param name="fieldName">password1</param>
- <param name="fieldName">password2</param>
- <param name="expression">password1==password2</param>
- <message>两次输入密码不一样!</message>
- </validator>
- <!-- birthday字段校验 -->
- <validator type="date">
- <param name="fieldName">birthday</param>
- <param name="min">1990-01-01</param>
- <param name="max">2011-04-14</param>
- <message>错误:生日应该在一个范围内!</message>
- </validator>
- </validators>
其他均不用修改,运行结果与前面相同!
上面配置文件中的type 即校验类型如下 这几个类型,是在com.opensymphony.xwork2.validator.validators.下面
default.xml文件里面 Struts2自带的校验器,代码如下:
- <?xml version="1.0" encoding="UTF-8"?>
- <!DOCTYPE validators PUBLIC
- "-//OpenSymphony Group//XWork Validator Config 1.0//EN"
- "http://www.opensymphony.com/xwork/xwork-validator-config-1.0.dtd">
- <!-- START SNIPPET: validators-default -->
- <validators>
- <validator name="required" class="com.opensymphony.xwork2.validator.validators.RequiredFieldValidator"/>
- <validator name="requiredstring" class="com.opensymphony.xwork2.validator.validators.RequiredStringValidator"/>
- <validator name="int" class="com.opensymphony.xwork2.validator.validators.IntRangeFieldValidator"/>
- <validator name="long" class="com.opensymphony.xwork2.validator.validators.LongRangeFieldValidator"/>
- <validator name="short" class="com.opensymphony.xwork2.validator.validators.ShortRangeFieldValidator"/>
- <validator name="double" class="com.opensymphony.xwork2.validator.validators.DoubleRangeFieldValidator"/>
- <validator name="date" class="com.opensymphony.xwork2.validator.validators.DateRangeFieldValidator"/>
- <validator name="expression" class="com.opensymphony.xwork2.validator.validators.ExpressionValidator"/>
- <validator name="fieldexpression" class="com.opensymphony.xwork2.validator.validators.FieldExpressionValidator"/>
- <validator name="email" class="com.opensymphony.xwork2.validator.validators.EmailValidator"/>
- <validator name="url" class="com.opensymphony.xwork2.validator.validators.URLValidator"/>
- <validator name="visitor" class="com.opensymphony.xwork2.validator.validators.VisitorFieldValidator"/>
- <validator name="conversion" class="com.opensymphony.xwork2.validator.validators.ConversionErrorFieldValidator"/>
- <validator name="stringlength" class="com.opensymphony.xwork2.validator.validators.StringLengthFieldValidator"/>
- <validator name="regex" class="com.opensymphony.xwork2.validator.validators.RegexFieldValidator"/>
- <validator name="conditionalvisitor" class="com.opensymphony.xwork2.validator.validators.ConditionalVisitorFieldValidator"/>
- </validators>
- <!-- END SNIPPET: validators-default -->
关于这几个校验器会在下文介绍。
本文转自 w156445045 51CTO博客,原文链接:http://blog.51cto.com/enetq/545259,如需转载请自行联系原作者