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

Jquery、 ajax和struts2+Hibernate查询数据库验证用户名是否存在,表单禁止提交等!

最近学习了ajax的异步调用和刷新技术,就实践了一把。也借鉴了别人的方法,再加入自己的思路,就有了下面的东东```有任何疑问,就留言吧````

首先是前台的jsp页面

关于jquery的post方法大家可以去看看这个,写的很详细:http://www.itivy.com/jquery/archive/2011/7/6/jquery-get-post-getjson-ajax.html

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@taglib uri="/struts-tags" prefix="s"%>
<%
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 'register.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">
	
	<script type="text/javascript" src="jquery-1.9.1.js"></script>//引进Jquery库
	
	 <script type="text/javascript">  
	 
    	jQuery(function()
    	{  
        	$("#username").blur(function()
        {  
            var username= $.trim($("#username").val());    
           $.post("checkusername.action",{username:username},function(date,state)
        	{
        	   if(date.result=="ok")
        	   {
        	   		$("#span1").html("<font color=\"red\">用户名可以使用</font>");
        	   		document.getElementById("submit").removeAttribute("disabled");
        	   		
        	   }else
        	   {
        		   $("#span1").html("<font color=\"red\">用户名不可以使用</font>");
        		   document.getElementById("submit").disabled = "true";
        	    }
        		   
        	   //alert(date.result);警告不好看,太吓人了
        	
        	return false;},'json');  
        });
    });   
    </script>  
	
  </head>
  
  <body>
   	<form action="savePerson.action" method="post" >
   	 
   	 username:<input type="text" name="username" size="20" id="username">
   	 <span id="span1"></span>
   	   <br>
   	 
   	 password:<input type="password" name="password" size="20"><br>
   	 age: <input type="text" name="age" size="20"><br>
   	 
   	 <input type="submit" value="提交" id="submit"  disabled="disabled">
   	 
   	
   	 
   	 
   	 
   
   </form>
   
  </body>
</html>
因为是使用的是struts2框架所以struts2里面加上如下代码:因为数据是采用接送传输的所以要写成extends="json-default"
<package name="strutsjson" extends="json-default">  
        <action name="checkusername"
         class="com.liumin.action.CheckUsername"  method="CheckPerson">  
             <result type="json"></result>  
        </action>  
    </package>  
action里面的代码如下

package com.liumin.action;

import java.util.List;

import com.liumin.model.Person;
import com.liumin.service.impl.CheckService;
import com.opensymphony.xwork2.ActionSupport;

public class CheckUsername extends ActionSupport
{
	private String username;//前台传来的数据username

	public String getUsername()
	{
		return username;