您的位置:首页 > Web前端 > JQuery

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

2013-03-24 08:52 1046 查看
最近学习了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;
}
public void setUsername(String username)
{
this.username = username;
}
private String result;

public String getResult()
{
return result;
}
public void setResult(String result)
{
this.result = result;
}

public String CheckPerson() throws Exception
{
CheckService checkService = new CheckService();

String str="select * from person where username='"+username+"'" ;//组建sql语句
List<Person> list=(List<Person>)checkService.getUser(str);//取得数据

if(list.isEmpty())//安条件查询后,判断list是否为空?
{
this.result = "ok";
}else
{
this.result = "no";
}
return SUCCESS;
}
}
service和dao层的我写到了一块:

package com.liumin.service.impl;

import java.util.List;

import org.hibernate.Session;
import org.hibernate.Transaction;

import com.liumin.model.Person;
import com.liumin.util.HibernateUtil;

public class CheckService
{
@SuppressWarnings("unchecked")
public List<Person> getUser(String str)
{
Session session = HibernateUtil.openSession();
Transaction tx = session.beginTransaction();
List<Person> list = null;
try
{
list = session.createSQLQuery(str).addEntity(Person.class).list();//查询数据
tx.commit();
}
catch (Exception e)
{
if (null != tx)
{
tx.rollback();
}
}
finally
{
HibernateUtil.close(session);
}
return list;
}
}
util里面的数据如下:

package com.liumin.util;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;

public class HibernateUtil
{
private static SessionFactory sessionFactory;

static
{
try
{
sessionFactory = new Configuration().configure().buildSessionFactory();
}
catch (Exception ex)
{
ex.printStackTrace();
}
}

public static Session openSession()
{
Session session = sessionFactory.openSession();

return session;
}

public static void close(Session session)
{
if(null != session)
{
session.close();
}
}
}
Hibernate的cfg和hbm代码就不用贴出来了!
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: