struts2学习笔记九(第9讲.Struts2的校验框架 续)
程序员文章站
2022-05-28 16:27:03
...
[b][size=xx-large]Struts2的校验框架 续[/size][/b]
struts2同样支持客户端的验证:
1、form的主题(theme)一定不能设为simple
一、修改register2.jsp中表单的主题( 去掉 theme="simple"):
2、将form的validate属性设置为true:
最好不要使用struts2给我们提供的客户端校验方法。
二、struts2的客户端校验,用自定义的js代码实现,修改register2.jsp页面:
[color=red]说明:[/color]struts2的校验分为字段校验和非字段校验,之前是使用的字段校验,字段校验与非字段检验最根本的区别:所谓的字段校验就是字段优先,字段校验是我去校验谁,我用谁来去校验;而非字段校验器是我用谁来校验,我去校验谁。
三、把sttuts2的字段校验修改成非字段校验,在RegisterAction-validate.xml中实现(去掉form表单中的onsubmit的方法):
[color=red]注意:[/color]一般的推荐是用字段的校验,以为这和前面的是一一对应的关系,比较的直观的查看校验信息,同时这两种校验形式是可共用的,但两者不推荐混着用。
[color=red]功能:[/color]field-validator有一个属性short-circuit,表示短路的意思,表示如果当前的验证错误了,后面的验证就不会进行验证了。
struts2同样支持客户端的验证:
1、form的主题(theme)一定不能设为simple
一、修改register2.jsp中表单的主题( 去掉 theme="simple"):
<s:form action="register">
2、将form的validate属性设置为true:
<s:form action="register" validate="true">
最好不要使用struts2给我们提供的客户端校验方法。
二、struts2的客户端校验,用自定义的js代码实现,修改register2.jsp页面:
<%@ page language="java" import="java.util.*" pageEncoding="GB18030"%>
<%@ 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 'register2.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">
-->
<script type="text/javascript">
function validate(){
var usernameValue = document.getElementById("usernameId").value;
var passwordValue = document.getElementById("passwordId").value;
var repasswordValue = document.getElementById("repasswordId").value;
if(usernameValue.length == 0){
alert("username should not be blank!");
return false;
}
else if(usernameValue.length < 6 || usernameValue.length > 10){
alert("length of username should be between 6 and 10!");
return false;
}
if(passwordValue.length == 0){
alert("password should not be blank!");
return false;
}
else if(passwordValue.length < 6 || passwordValue.length > 10){
alert("length of password should be between 6 and 10!");
return false;
}
if(repasswordValue.length == 0){
alert("repassword should not be blank!");
return false;
}
else if(repasswordValue.length < 6 || repasswordValue.length > 10){
alert("length of repassword should be between 6 and 10!");
return false;
}
if(passwordValue != repasswordValue){
alert("password and repassword should be same!");
return false;
}
return true;
}
</script>
</head>
<body>
<table align="center" width="40%">
<tr>
<td>
<s:fielderror cssStyle="color:red"/>
</td>
</tr>
</table>
<s:form action="register" theme="simple" onsubmit="return validate();">
<table align="center" width="40%" border="1">
<tr>
<td>username
</td>
<td>
<s:textfield name="username" label="username" id="usernameId"></s:textfield>
</td>
</tr>
<tr><td>password</td>
<td>
<s:password name="password" label="password" id="passwordId"></s:password>
</td>
</tr>
<tr><td>repassword</td>
<td>
<s:password name="repassword" label="repassword" id="repasswordId"></s:password>
</td>
</tr>
<tr><td>age</td>
<td>
<s:textfield name="age" label="age"></s:textfield>
</td>
</tr>
<tr><td>birthday</td>
<td>
<s:textfield name="birthday" label="birthday"></s:textfield>
</td>
</tr>
<tr><td>graduation</td>
<td>
<s:textfield name="graduation" label="graduation"></s:textfield>
</td>
</tr>
<tr><td><s:submit value=" submit "></s:submit></td>
<td>
<s:reset value=" reset "></s:reset>
</td>
</tr>
</table>
</s:form>
</body>
</html>
[color=red]说明:[/color]struts2的校验分为字段校验和非字段校验,之前是使用的字段校验,字段校验与非字段检验最根本的区别:所谓的字段校验就是字段优先,字段校验是我去校验谁,我用谁来去校验;而非字段校验器是我用谁来校验,我去校验谁。
三、把sttuts2的字段校验修改成非字段校验,在RegisterAction-validate.xml中实现(去掉form表单中的onsubmit的方法):
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE validators PUBLIC "-//OpenSymphony Group//XWork Validator 1.0.2//EN" "http://www.opensymphony.com/xwork/xwork-validator-1.0.2.dtd">
<validators >
<validator type="requiredstring">
<param name="fieldName">username</param>
<message>username should not be blank!!!</message>
</validator>
<validator type="stringlength">
<param name="fieldName">username</param>
<param name="minLength">6</param>
<param name="maxLength">10</param>
<message>username should be between ${minLength} and ${maxLength}</message>
</validator>
<!-- struts2的自动校验
<field name="username">
<field-validator type="requiredstring">
<param name="trim">true</param>
<message>username should not be blank!!!</message>
</field-validator>
<field-validator type="stringlength">
<param name="minLength">6</param>
<param name="maxLength">10</param>
<message>username should be between ${minLength} and ${maxLength}</message>
</field-validator>
</field>
<field name="password">
<field-validator type="requiredstring">
<message>password should not be blank!!!</message>
</field-validator>
<field-validator type="stringlength">
<param name="minLength">6</param>
<param name="maxLength">10</param>
<message>password should be between ${minLength} and ${maxLength}</message>
</field-validator>
</field>
<field name="repassword">
<field-validator type="requiredstring">
<message>repassword should not be blank!!!</message>
</field-validator>
<field-validator type="stringlength">
<param name="minLength">6</param>
<param name="maxLength">10</param>
<message>repassword should be between ${minLength} and ${maxLength}</message>
</field-validator>
</field>
<field name="age">
<field-validator type="required">
<message>age should not be blank!!!</message>
</field-validator>
<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="required">
<message>birthday should not be blank!!!</message>
</field-validator>
<field-validator type="date">
<param name="min">2001-01-01</param>
<param name="max">2003-12-31</param>
<message>birthday should be between ${min} and ${max}</message>
</field-validator>
</field>
<field name="graduation">
<field-validator type="required">
<message>graduation should be not blank!!!</message>
</field-validator>
<field-validator type="date">
<param name="min">2005-01-01</param>
<param name="max">2007-12-31</param>
<message>graduation should be between ${min} and ${max}</message>
</field-validator>
</field>
-->
</validators>
[color=red]注意:[/color]一般的推荐是用字段的校验,以为这和前面的是一一对应的关系,比较的直观的查看校验信息,同时这两种校验形式是可共用的,但两者不推荐混着用。
[color=red]功能:[/color]field-validator有一个属性short-circuit,表示短路的意思,表示如果当前的验证错误了,后面的验证就不会进行验证了。
上一篇: Hessian 使用入门