日期:2014-05-16  浏览次数:20373 次

使用Struts1的validator框架生成JavaScript验证代码

有了前台的javaScript校验,为什么要使用Validator框架进行后台校验呢?因为为了安全。有些黑客会绕过前台js校验;而且Validator框架也可以生成对应js代码;

下面我就把写好的validator验证框架转换成相应的JS代码,只要一条语句,非常方便:

register.jsp文件代码如下:

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib uri="http://struts.apache.org/tags-html" prefix="html"%>
<%@ taglib uri="http://struts.apache.org/tags-bean" prefix="bean"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>用户注册</title>
	<html:javascript formName="registerForm" method="validRegister"/>
</head>
<body>
	<center>
		
		<table>
			<html:form action="/Register" method="POST" onsubmit="return validRegister(this)">
			<tr><td colspan="2" align="center"><bean:message key="form.userRegister"/></td></tr>
			<tr>
				<td><bean:message key="form.username"/>:</td>
				<td><html:text property="username"></html:text></td>
				<td><html:errors property="username"/></td>
			</tr>
			<tr>
				<td><bean:message key="form.password"/>:</td>
				<td><html:text property="password"></html:text></td>
				<td><html:errors property="password"/></td>
			</tr>
			<tr>
				<td><bean:message key="form.password2"/>:</td>
				<td><html:text property="password2"></html:text></td>
				<td><html:errors property="password2" /></td>
			</tr>
			<tr>
				<td><bean:message key="form.email"/>:</td>
				<td><html:text property="email"></html:text></td>
				<td><html:errors property="email"/></td>
			</tr>
			<tr>
				<td align="center" colspan="3">
					<html:submit><bean:message key="form.submit"></bean:message></html:submit>
					<html:reset><bean:message key="form.reset"></bean:message></html:reset>
				</td>
			</tr>
			</html:form>
		</table>
			
	</center>
</body>
</html>

?在<title>标签下面的<html:javascript>标签表示要生成JavaScript代码,当只有formName时,那么在表单中要使用

<html:form action="/Register" method="POST" onsubmit="return validateRegisterForm
(this)">

对JS进行调用。

如果同时指定了method方法,那么在表单中使用onsubmit="return validRegister"对JS进行调用。

?

如下是转换之后的JS代码:

<script type="text/javascript" language="Javascript1.1"> 

<!-- Begin 

    var bCancel = false; 

    function validRegister(form) { 
        if (bCancel) { 
            return true; 
        } else { 
            var formValidationResult; 
            formValidationResult = validateRequired(form) && validateMinLength(form) && validateMaxLength(form) && validateEmail(form); 
            return (formValidationResult); 
        } 
    } 

    function registerForm_required () { 
     this.a0 = new Array("username", "用户名 是必须的.", new Function ("varName", "this.minlength='3'; this.maxlength='7';  return this[varName];"));
     this.a1 = new Array("password", "密码 是必须的.", new Function ("varName", "this.minlength='3'; this.maxlength='7';  return this[varName];"));
     this.a2 = new Array("password2", "密码确认 是必须的.", new Function ("varName", "this.test='((password == null) or (*this* == password))';  return this[varName];"));
    } 

    function registerForm_minlength () { 
     this.a0 = new Array("username", "用户名 不能小于 3 字符.", new Function ("varName", "this.minlength='3'; this.maxlength='7';  return this[varName];"));
     this.a1 = new Ar