- 浏览: 108208 次
- 性别:
- 来自: 北京
文章分类
最新评论
-
zhanggen515200:
哎哟不错喔
freemarker截取字符串subString -
ssy341:
下载来试试,学习学习~~~
activemq学习(1) -
yaofaye:
was下如何调整呢?
does not support XML Schema. Are you running on Java 1.4 with Apache Crimson -
hxl1013:
...
freemarker截取字符串subString -
ieblaze:
verynice
freemarker截取字符串subString
通过继承ActionSupport重写validate()方法,实现数据校验,会存在大量的重复代码,
类似于struts1,Struts2也提供通过配置文件来进行数据校验,使用校验框架不必重复的编写校验代码.
Action及jsp可以不用改变
Java代码
Struts.xml
Xml代码
jsp
Html代码
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@taglib prefix="s" uri ="/struts-tags"%>
<html>
<body>
<form action="regist.action" method="post">
<s:fielderror></s:fielderror>
<table>
<tr>
<td>userName:</td>
<td>
<input type="text" name="userName">
</td>
</tr>
<tr>
<td>password:</td>
<td>
<input type="text" name="password">
</td>
</tr>
<tr>
<td>age:</td>
<td>
<input type="text" name="age">
</td>
</tr>
<tr>
<td>birthday:</td>
<td>
<input type="text" name="birthday">
</td>
</tr>
<tr>
<td colspan="2">
<s:submit value="注册"></s:submit>
</td>
</tr>
</form>
</body>
</html>
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@taglib prefix="s" uri ="/struts-tags"%>
<html>
<body>
<form action="regist.action" method="post">
<s:fielderror></s:fielderror>
<table>
<tr>
<td>userName:</td>
<td>
<input type="text" name="userName">
</td>
</tr>
<tr>
<td>password:</td>
<td>
<input type="text" name="password">
</td>
</tr>
<tr>
<td>age:</td>
<td>
<input type="text" name="age">
</td>
</tr>
<tr>
<td>birthday:</td>
<td>
<input type="text" name="birthday">
</td>
</tr>
<tr>
<td colspan="2">
<s:submit value="注册"></s:submit>
</td>
</tr>
</form>
</body>
</html>
添加验证文件
在RegistAction.java同目录下添加文件名为RegistAction-validation.xml(action名-validation.xml)
Xml代码
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE validators PUBLIC
"-//OpenSymphony Group//XWork Validator 1.0//EN"
"http://www.opensymphony.com/xwork/xwork-validator-1.0.2.dtd">
<validators>
<field name="userName">
<!-- 验证字符串不能为空 -->
<field-validator type="requiredstring">
<!-- 去空格 -->
<param name="trim">true</param>
<!-- 错误提示信息 -->
<message>userName is required</message>
</field-validator>
<!-- 验证字符串长度 -->
<field-validator type="stringlength">
<param name="minLength">5</param>
<param name="maxLength">16</param>
<message>userName should be between ${minLength} and ${maxLength}</message>
</field-validator>
</field>
<field name="password">
<field-validator type="requiredstring">
<param name="trim">true</param>
<message>password is required</message>
</field-validator>
<field-validator type="stringlength">
<param name="minLength">5</param>
<param name="maxLength">16</param>
<message>password should be between ${minLength} and ${maxLength}</message>
</field-validator>
</field>
<!-- 验证字符串为数字 -->
<field name="age">
<field-validator type="int">
<param name="min">1</param>
<param name="max">150</param>
<message>age should be between ${min} and ${max}</message>
</field-validator>
</field>
<!-- 验证字符串为日期类型 -->
<field name="birthday">
<field-validator type="date">
<param name="min">1888-01-01</param>
<param name="max">2008-01-01</param>
<message>birthday should be between ${min} and ${max}</message>
</field-validator>
</field>
</validators>
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE validators PUBLIC
"-//OpenSymphony Group//XWork Validator 1.0//EN"
"http://www.opensymphony.com/xwork/xwork-validator-1.0.2.dtd">
<validators>
<field name="userName">
<!-- 验证字符串不能为空 -->
<field-validator type="requiredstring">
<!-- 去空格 -->
<param name="trim">true</param>
<!-- 错误提示信息 -->
<message>userName is required</message>
</field-validator>
<!-- 验证字符串长度 -->
<field-validator type="stringlength">
<param name="minLength">5</param>
<param name="maxLength">16</param>
<message>userName should be between ${minLength} and ${maxLength}</message>
</field-validator>
</field>
<field name="password">
<field-validator type="requiredstring">
<param name="trim">true</param>
<message>password is required</message>
</field-validator>
<field-validator type="stringlength">
<param name="minLength">5</param>
<param name="maxLength">16</param>
<message>password should be between ${minLength} and ${maxLength}</message>
</field-validator>
</field>
<!-- 验证字符串为数字 -->
<field name="age">
<field-validator type="int">
<param name="min">1</param>
<param name="max">150</param>
<message>age should be between ${min} and ${max}</message>
</field-validator>
</field>
<!-- 验证字符串为日期类型 -->
<field name="birthday">
<field-validator type="date">
<param name="min">1888-01-01</param>
<param name="max">2008-01-01</param>
<message>birthday should be between ${min} and ${max}</message>
</field-validator>
</field>
</validators>
Struts 2.0已经实现很多常用的校验了,在jar的default.xml中的注册的
Xml代码
<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 ="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" />
</validators>
<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 ="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" />
</validators>
<message>提示信息也可以国际化,<message key=””>就可以显示资源文件里的key对应的value了
当一个Action中有多个业务方法时,每个业务方法都会使用这种验证,显然是不好的.例如Action中有方法public String regist(),我们可以为regist()专门定义一个配置文件,文件名称为RegistAction-regist-validation.xml(Action名称-方法名- validation.xml)
类似于struts1,Struts2也提供通过配置文件来进行数据校验,使用校验框架不必重复的编写校验代码.
Action及jsp可以不用改变
Java代码
package com; import java.util.Date; import com.opensymphony.xwork2.ActionSupport; public class RegistAction extends ActionSupport { private String userName; private String password; private Integer age; private Date birthday; public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } public Date getBirthday() { return birthday; } public void setBirthday(Date birthday) { this.birthday = birthday; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public String getUserName() { return userName; } public void setUserName(String userName) { this.userName = userName; } @Override public String execute() throws Exception { return SUCCESS; } } package com; import java.util.Date; import com.opensymphony.xwork2.ActionSupport; public class RegistAction extends ActionSupport { private String userName; private String password; private Integer age; private Date birthday; public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } public Date getBirthday() { return birthday; } public void setBirthday(Date birthday) { this.birthday = birthday; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public String getUserName() { return userName; } public void setUserName(String userName) { this.userName = userName; } @Override public String execute() throws Exception { return SUCCESS; } }
Struts.xml
Xml代码
<?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> <constant name="struts.custom.i18n.resources" value="messageResource"></constant> <package name="user" extends="struts-default"> <action name="regist" class="com.RegistAction"> <result name="success">/welcome.jsp</result> <result name="input">/regist.jsp</result> </action> </package> </struts> <?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> <constant name="struts.custom.i18n.resources" value="messageResource"></constant> <package name="user" extends="struts-default"> <action name="regist" class="com.RegistAction"> <result name="success">/welcome.jsp</result> <result name="input">/regist.jsp</result> </action> </package> </struts>
jsp
Html代码
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@taglib prefix="s" uri ="/struts-tags"%>
<html>
<body>
<form action="regist.action" method="post">
<s:fielderror></s:fielderror>
<table>
<tr>
<td>userName:</td>
<td>
<input type="text" name="userName">
</td>
</tr>
<tr>
<td>password:</td>
<td>
<input type="text" name="password">
</td>
</tr>
<tr>
<td>age:</td>
<td>
<input type="text" name="age">
</td>
</tr>
<tr>
<td>birthday:</td>
<td>
<input type="text" name="birthday">
</td>
</tr>
<tr>
<td colspan="2">
<s:submit value="注册"></s:submit>
</td>
</tr>
</form>
</body>
</html>
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@taglib prefix="s" uri ="/struts-tags"%>
<html>
<body>
<form action="regist.action" method="post">
<s:fielderror></s:fielderror>
<table>
<tr>
<td>userName:</td>
<td>
<input type="text" name="userName">
</td>
</tr>
<tr>
<td>password:</td>
<td>
<input type="text" name="password">
</td>
</tr>
<tr>
<td>age:</td>
<td>
<input type="text" name="age">
</td>
</tr>
<tr>
<td>birthday:</td>
<td>
<input type="text" name="birthday">
</td>
</tr>
<tr>
<td colspan="2">
<s:submit value="注册"></s:submit>
</td>
</tr>
</form>
</body>
</html>
添加验证文件
在RegistAction.java同目录下添加文件名为RegistAction-validation.xml(action名-validation.xml)
Xml代码
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE validators PUBLIC
"-//OpenSymphony Group//XWork Validator 1.0//EN"
"http://www.opensymphony.com/xwork/xwork-validator-1.0.2.dtd">
<validators>
<field name="userName">
<!-- 验证字符串不能为空 -->
<field-validator type="requiredstring">
<!-- 去空格 -->
<param name="trim">true</param>
<!-- 错误提示信息 -->
<message>userName is required</message>
</field-validator>
<!-- 验证字符串长度 -->
<field-validator type="stringlength">
<param name="minLength">5</param>
<param name="maxLength">16</param>
<message>userName should be between ${minLength} and ${maxLength}</message>
</field-validator>
</field>
<field name="password">
<field-validator type="requiredstring">
<param name="trim">true</param>
<message>password is required</message>
</field-validator>
<field-validator type="stringlength">
<param name="minLength">5</param>
<param name="maxLength">16</param>
<message>password should be between ${minLength} and ${maxLength}</message>
</field-validator>
</field>
<!-- 验证字符串为数字 -->
<field name="age">
<field-validator type="int">
<param name="min">1</param>
<param name="max">150</param>
<message>age should be between ${min} and ${max}</message>
</field-validator>
</field>
<!-- 验证字符串为日期类型 -->
<field name="birthday">
<field-validator type="date">
<param name="min">1888-01-01</param>
<param name="max">2008-01-01</param>
<message>birthday should be between ${min} and ${max}</message>
</field-validator>
</field>
</validators>
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE validators PUBLIC
"-//OpenSymphony Group//XWork Validator 1.0//EN"
"http://www.opensymphony.com/xwork/xwork-validator-1.0.2.dtd">
<validators>
<field name="userName">
<!-- 验证字符串不能为空 -->
<field-validator type="requiredstring">
<!-- 去空格 -->
<param name="trim">true</param>
<!-- 错误提示信息 -->
<message>userName is required</message>
</field-validator>
<!-- 验证字符串长度 -->
<field-validator type="stringlength">
<param name="minLength">5</param>
<param name="maxLength">16</param>
<message>userName should be between ${minLength} and ${maxLength}</message>
</field-validator>
</field>
<field name="password">
<field-validator type="requiredstring">
<param name="trim">true</param>
<message>password is required</message>
</field-validator>
<field-validator type="stringlength">
<param name="minLength">5</param>
<param name="maxLength">16</param>
<message>password should be between ${minLength} and ${maxLength}</message>
</field-validator>
</field>
<!-- 验证字符串为数字 -->
<field name="age">
<field-validator type="int">
<param name="min">1</param>
<param name="max">150</param>
<message>age should be between ${min} and ${max}</message>
</field-validator>
</field>
<!-- 验证字符串为日期类型 -->
<field name="birthday">
<field-validator type="date">
<param name="min">1888-01-01</param>
<param name="max">2008-01-01</param>
<message>birthday should be between ${min} and ${max}</message>
</field-validator>
</field>
</validators>
Struts 2.0已经实现很多常用的校验了,在jar的default.xml中的注册的
Xml代码
<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 ="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" />
</validators>
<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 ="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" />
</validators>
<message>提示信息也可以国际化,<message key=””>就可以显示资源文件里的key对应的value了
当一个Action中有多个业务方法时,每个业务方法都会使用这种验证,显然是不好的.例如Action中有方法public String regist(),我们可以为regist()专门定义一个配置文件,文件名称为RegistAction-regist-validation.xml(Action名称-方法名- validation.xml)
发表评论
-
struts2 FilterDispatcher 和 StrutsPrepareAndExecuteFilter 的区别
2011-06-01 21:09 1410FilterDispatcher是struts2.0.x到2. ... -
struts2的异常处理
2009-07-15 09:13 1026异常处理后,能够提供友好的用户界面,而不是一些错误的代码信息. ... -
Struts2 国际化
2009-05-11 22:22 12481、应用基础 (国际化Internationalizatio ... -
Struts2学习:类型转换
2009-05-11 14:08 1179框架中常规的类型转换 ... -
Struts2 校验
2009-05-11 12:31 1164struts2的输入校验 类型 ... -
Struts2拦截器的使用
2009-05-11 09:06 1567如何使用struts2拦截器,或者自定义拦截器。特别注意,在使 ...
相关推荐
Struts2 验证框架详解 Struts2 验证框架是基于 Java 语言的 Web 应用程序框架,提供了一个强大的验证机制,以确保用户输入的数据满足业务逻辑的要求。在 Struts2 中,验证机制是通过 validator 来实现的,该机制...
Struts2作为一款流行的Java Web开发框架,提供了一套强大的校验框架,使得开发者能够方便地对用户输入数据进行验证,确保数据的准确性和安全性。本文将深入探讨Struts2校验框架的应用,包括其核心概念、配置方式、...
其中,Struts2提供的校验框架是其核心特性之一,能够有效地进行表单验证,并且支持多种验证方式。 #### 二、配置Struts2框架 为了使用Struts2框架,首先需要在项目的`web.xml`文件中配置过滤器。下面的示例展示了...
在Struts2中,验证框架是其核心特性之一,它允许开发者对用户输入的数据进行校验,确保数据的完整性和安全性。这个“struts2验证框架示例”提供了深入理解并实际操作Struts2验证功能的机会。 首先,让我们了解一下...
### Struts2校验框架详解 #### 一、概述 Struts2作为一款流行的Java Web应用框架,提供了丰富的功能来支持开发人员构建高效且可维护的应用程序。其中,Struts2校验框架(Validation Framework)是该框架的核心特性...
在Struts2中,验证框架是处理用户输入验证的关键部分,它确保了从客户端接收到的数据的质量和准确性。本示例将深入探讨Struts2验证框架的基本用法,特别适合初学者理解和掌握。 ### 一、Struts2验证框架概述 ...
在Struts2框架中,验证框架是其一个重要组成部分,用于确保输入数据的准确性和完整性,从而防止因无效数据导致的程序异常或错误。本实例将带你深入理解Struts2验证框架的使用,以及在实际操作中可能遇到的问题和解决...
Struts2的验证框架主要负责在用户提交表单时,对输入数据进行检查。它通过定义验证规则,如非空、长度限制、格式验证等,来防止无效或恶意的数据进入系统。验证框架可以减少后端业务逻辑中的验证代码,提高代码的...
Struts2验证框架是Java开发中的一个关键组件,主要用于处理Web应用中的用户输入验证。它提供了灵活且强大的机制,确保用户提交的数据符合预期格式和业务规则。Struts2的验证框架是基于Action类和XML配置文件的,可以...
验证框架是Struts2中的一个重要组成部分,用于处理用户输入的数据验证。在这个框架中,验证逻辑与业务逻辑分离,使得代码更易于管理和扩展。 在Struts2验证框架中,主要有两种验证方式:客户端验证和服务器端验证。...
在Struts2中,验证框架是其核心特性之一,它负责确保用户输入的数据符合预定义的规则,从而保证数据的准确性和安全性。本文将深入探讨Struts2验证框架的使用和扩展。 1. **Struts2验证框架基础** - **Action与...
Struts2提供了内置的验证框架,允许开发者自定义校验规则,以满足特定业务需求。下面将详细介绍Struts2自定义校验框架的相关知识点。 1. **Struts2验证框架概述** Struts2的验证框架主要负责处理用户提交的数据,...
2、用户名只能使用数字或者字母,长度为6~20之间; 3、密码、确认密码必须是数字和字母,长度为6~20之间; 4、密码、确认密码必须相同; 5、收入必须在0.001~10000.000之间; 6、年龄必须是整数且必须在0~120...