struts2学习之七校验框架--字段校验

struts2学习之七校验框架--字段校验(使用xml文件)
一、xml文件规范

在相应的action的同一包下×××--validation.xml
如RegisterAction对应的校验框架为RegisterAction-validation.xml

若要写对应某个方法的写法如下:   RegisterAction-test-validation.xml
当执行该方法时校验顺序RegisterAction-test-validation.xml--->RegisterAction-validation.xml

所以当有多个方法时,最好不要提供总的xml了,这样比较清晰

 

二、可以根据app中的范例获得xml头部如何写
<?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">

三、可以用firefox打开上面链接中的dtd
上面有对如何写dtd的约束

四、写按“field”的校验框架(一个filed对应一个字段)

 

 <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">

<!--此处日期应该是required而不是requiredString,否则会有多余的错误信息,requiredString只适用于字符串-->
   <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 not be 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>

  
以上书写的属性值名字如requiredstring可参考xwork.JAR中的vlidators包中default.xml文件中的name


五、校验框架校验错误的信息是放在fieldError里面的,不是放在actionError里的。


当校验信息不复杂推荐用这种xml方式,若特复杂才用validate的方法

 

六、web开发客户端校验
1、客户端校验javascript
2、服务器端校验-前面阐述的校验都是服务器端校验
3、struts2同样支持客户端校验,但还是比较弱的,比较死板,一般自己编写javascript
用struts2支持客户端校验  

要求:1)form的主题(theme)一定不能设置为simple
         2)将form的validate属性设置为true,会将我们写的xml文件翻译成javascript文件生成在客户端校验。

 4、自己编写javascript校验举例

 以下图标分别对应了方法申明处和调用处

<%@ 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()
	{
	<!--document.getElementsByName("username")[0]写[0]的原因前面的函数返回的是一个数组-->
	<!--document.getElementsByName("username")[0]等价于document.getElementById("usernameId").value-->
		var usernameValue = document.getElementById("usernameId").value;
		var passwordValue = document.getElementById("password").value;
		var repasswordValue = document.getElementById("repassword").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>
						re-password
					</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>

 

 

你可能感兴趣的:(JavaScript,xml,框架,jsp,css)