jqueryvalidate(猫冬插件)+spring3.0+struts2.18(集成开发)

来源:互联网 发布:印度用手擦屁股 知乎 编辑:程序博客网 时间:2024/04/30 19:24

发起请求的首页:index.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"%>

<%

String path = request.getContextPath();

String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";

%>

<%@ include file="/WEB-INF/pages/common/taglibs.jsp"%>

<!--<%@taglib prefix="decorator"

uri="http://www.opensymphony.com/sitemesh/decorator"%>

--><!--<%@taglib prefix="page" uri="http://www.opensymphony.com/sitemesh/page"%>

--><s:url action="regist" namespace="/" includeParams="none" id="regist"></s:url>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<html>

<head>

<title>登录和注册</title>

<script type="text/javascript" src="${context_path }/scripts/jquery.js"></script>

<script type="text/javascript" src="${context_path }/scripts/common.js"></script>

<script type="text/javascript" src="${context_path }/scripts/reflection.js"></script>

<link rel="stylesheet" type="text/css" href="styles/common.css" />

 <base href="<%=basePath%>">

<script type="text/javascript">

$(function(){

$("a").click(function(){

var hr = $(this).attr("href");

alert($(this).index())

var hrcon = $("#//"+hr).html();

$("#show_div").html(hrcon);

})

})

</script>

 

</head>

<body style="background-color: black;">

<dl  id="nav">

<dd><div style="background-color:black;width: 100%"><a href="${regist}">注册</a> | <a href="#">登录</a></div></dd>

<dd></dd>

</dl>

<div style="width: 100%;height: 100%;border: 1px solid red" id="show_div"></div>

</body>

</html>

使用jquery校验的页面:register.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"%>

<%@ include file="/WEB-INF/pages/common/taglibs.jsp"%>

<%

String path = request.getContextPath();

String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";

%>

<s:url action="validateUserName" namespace="/reg"  includeParams="none" id="validateUserName"></s:url>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<html>

<head>

 <base href="<%=basePath%>">

<title>Register</title>

<script src="scripts/validate/jquery_last.js" type="text/javascript"></script>

<link type="text/css" rel="stylesheet" href="styles/validator.css"></link>

<script src="scripts/validate/formValidator_min.js" type="text/javascript"></script>

<script src="scripts/validate/formValidatorRegex.js" type="text/javascript"></script>

<script language="javascript" src="scripts/validate/DateTimeMask.js" type="text/javascript"></script>

<script type="text/javascript">

$(document).ready(function(){

$.formValidator.initConfig({onError:function(msg){alert(msg)}});

$("#username").formValidator({onshow:"请输入用户名",onfocus:"用户名至少4个字符,最多10个字符",oncorrect:"该用户名可以注册"}).InputValidator({min:4,max:10,onerror:"你输入的用户名非法,请确认"}).RegexValidator({regexp:"username",datatype:"enum",onerror:"用户名格式不正确"})

   .AjaxValidator({

   type : "get",

url : "${validateUserName}",

datatype : "json",

success : function(data){

           return data.responseText == "1";

},

buttons: $("#button"),

error: function(){alert("服务器没有返回数据,可能服务器忙,请重试");},

onerror : "该用户名不可用,请更换用户名",

onwait : "正在对用户名进行合法性校验,请稍候..."

}).DefaultPassed();

$("#password1").formValidator({onshow:"请输入密码",onfocus:"密码不能为空",oncorrect:"密码合法"}).InputValidator({min:1,onerror:"密码不能为空,请确认"});

$("#password2").formValidator({onshow:"请输入重复密码",onfocus:"两次密码必须一致哦",oncorrect:"密码一致"}).InputValidator({min:1,onerror:"重复密码不能为空,请确认"}).CompareValidator({desID:"password1",operateor:"=",onerror:"2次密码不一致,请确认"});

});

 

</script>

 

</head>

<body>

<s:form action="" onsubmit="return jQuery.formValidator.PageIsValid('1')" theme="simple">

<table>

<tr>

<td>用户帐号</td><td><s:textfield name="user.name" id="username"/></td><td><div id="usernameTip"></div></td>

</tr>

<tr>

<td>用户密码</td><td><s:textfield name="user.password" id="password1"/></td><td><div id="password1Tip"></div></td>

</tr>

<tr>

<td>用户密码</td><td><s:textfield id="password2"/></td><td><div id="password2Tip"></div></td>

</tr>

<tr>

<td>QQ:</td><td><s:textfield name="user.qq" id="qq"/></td><td><div id="qqTip"></div></td>

</tr>

<tr>

<td>联系电话</td><td><s:textfield name="user.tel" id="tel"/></td><td><div id="telTip"></div></td>

</tr>

<tr>

<td>备注</td><td><s:textfield name="user.remark" id="remark"/></td><td><div id="remarkTip"></div></td>

</tr>

</table>

</s:form>

</body>

 

</html>

被嵌入的标记页面:taglibs.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"%>

<%@ taglib prefix="s" uri="/struts-tags"%>

 

请求的action:RegistAction

package common.regist.action;

 

import com.opensymphony.xwork2.ActionSupport;

import common.regist.Interface.IRegistService;

import domain.User;

 

public class RegistAction extends ActionSupport {

 

private IRegistService service;

private String responseText;

private String username;

private User user;

 

/**

* @return the service

*/

public IRegistService getService() {

return service;

}

 

/**

* @param service

*            the service to set

*/

public void setService(IRegistService service) {

this.service = service;

}

 

/**

* @return the user

*/

public User getUser() {

return user;

}

 

/**

* @param user the user to set

*/

public void setUser(User user) {

this.user = user;

}

public String validateUserName() {

boolean exist = service.validateUser(username);

this.setResponseText(exist?"1":"0");

return ActionSupport.SUCCESS;

}

 

/**

* @return the responseText

*/

public String getResponseText() {

return responseText;

}

 

/**

* @param responseText the responseText to set

*/

public void setResponseText(String responseText) {

this.responseText = responseText;

}

 

public void setUsername(String username) {

this.username = username;

}

 

}

struts.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>

<package name="index" extends="struts-default" namespace="/">

<action name="index">

<result>/WEB-INF/welcome.jsp</result>

</action>

<action name="regist">

<result>/WEB-INF/pages/common/register.jsp</result>

</action>

</package>

<package name="regist" extends="json-default" namespace="/reg">

<action name="validateUserName" class="registAction"

method="validateUserName">

<result type="json">

<param name="excludeProperties">

service,user

</param>

</result>

</action>

</package>

</struts>

 

完成资源见:http://dushoujuanqingtian.download.csdn.net/,本实例承蒙http://www.gbsou.com/版主的大量协助,非常感激!!!

原创粉丝点击