使用AJAX完成用户名是否存在异步校验

所属分类: 网络编程 / AJAX相关 阅读数: 568
收藏 0 赞 0 分享

使用AJAX完成用户名是否存在异步校验:

1.事件触发:

* onblur

2.编写AJAX代码:

* 项Action中提交:传递username参数

3.编写Action

* 接收username:模型驱动接收.

4.* 编写实体类

* User

* User.hbm.xml

* 配置到spring中.

5.编写DAO

* 继承HibernateDaoSupport

* 在配置中注入sessionFactory

6.编写Service:

* 注入UserDao

* 事务管理:

核心代码实现:

function checkUsername(){ 
    // 获得文件框值: 
    var username = document.getElementById("username").value; 
    // 1.创建异步交互对象 
    var xhr = createXmlHttp(); 
    // 2.设置监听 
    xhr.onreadystatechange = function(){ 
      if(xhr.readyState == 4){ 
        if(xhr.status == 200){ 
          document.getElementById("span1").innerHTML = xhr.responseText; 
        } 
      } 
    } 
    // 3.打开连接 
    xhr.open("GET","${pageContext.request.contextPath}/user_findByName.action?time="+new Date().getTime()+"&username="+username,true); 
    // 4.发送 
    xhr.send(null); 
  } 
  function createXmlHttp(){ 
      var xmlHttp; 
      try{ // Firefox, Opera 8.0+, Safari 
        xmlHttp=new XMLHttpRequest(); 
      } 
      catch (e){ 
        try{// Internet Explorer 
           xmlHttp=new ActiveXObject("Msxml2.XMLHTTP"); 
         } 
        catch (e){ 
         try{ 
           xmlHttp=new ActiveXObject("Microsoft.XMLHTTP"); 
         } 
         catch (e){} 
         } 
      } 
      return xmlHttp; 
     } 
public String findByName() throws IOException { 
    // 调用Service进行查询: 
    User existUser = userService.findByUsername(user.getUsername()); 
    // 获得response对象,项页面输出: 
    HttpServletResponse response = ServletActionContext.getResponse(); 
    response.setContentType("text/html;charset=UTF-8"); 
    // 判断 
    if (existUser != null) { 
      // 查询到该用户:用户名已经存在 
      response.getWriter().println("<font color='red'>用户名已经存在</font>"); 
    } else { 
      // 没查询到该用户:用户名可以使用 
      response.getWriter().println("<font color='green'>用户名可以使用</font>"); 
    } 
    return NONE; 
  } 
private UserDao userDao; 
  public void setUserDao(UserDao userDao) { 
    this.userDao = userDao; 
  } 
  // 按用户名查询用户的方法: 
  public User findByUsername(String username){ 
    return userDao.findByUsername(username); 
  } 
public User findByUsername(String username){ 
    String hql = "from User where username = ?"; 
    List<User> list = this.getHibernateTemplate().find(hql, username); 
    if(list != null && list.size() > 0){ 
      return list.get(0); 
    } 
    return null; 
  } 
<?xml version="1.0" encoding="UTF-8"?> 
<beans xmlns="http://www.springframework.org/schema/beans" 
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
  xmlns:context="http://www.springframework.org/schema/context" 
  xmlns:aop="http://www.springframework.org/schema/aop" 
  xmlns:tx="http://www.springframework.org/schema/tx" 
  xsi:schemaLocation="http://www.springframework.org/schema/beans  
  http://www.springframework.org/schema/beans/spring-beans.xsd 
  http://www.springframework.org/schema/context 
  http://www.springframework.org/schema/context/spring-context.xsd 
  http://www.springframework.org/schema/aop 
  http://www.springframework.org/schema/aop/spring-aop.xsd 
  http://www.springframework.org/schema/tx  
  http://www.springframework.org/schema/tx/spring-tx.xsd"> 
  <!-- 配置连接池: --> 
  <!-- 引入外部属性文件 --> 
  <context:property-placeholder location="classpath:jdbc.properties"/> 
  <!-- 配置C3P0连接池: --> 
  <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"> 
    <property name="driverClass" value="${jdbc.driver}"/> 
    <property name="jdbcUrl" value="${jdbc.url}"/> 
    <property name="user" value="${jdbc.user}"/> 
    <property name="password" value="${jdbc.password}"/> 
  </bean> 
  <!-- Hibernate的相关信息 --> 
  <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"> 
    <!-- 注入连接池 --> 
    <property name="dataSource" ref="dataSource"/> 
    <!-- 配置Hibernate的其他的属性 --> 
    <property name="hibernateProperties"> 
      <props> 
        <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop> 
        <prop key="hibernate.show_sql">true</prop> 
        <prop key="hibernate.format_sql">true</prop> 
        <prop key="hibernate.connection.autocommit">false</prop> 
        <prop key="hibernate.hbm2ddl.auto">update</prop> 
      </props> 
    </property> 
    <!-- 配置Hibernate的映射文件 --> 
    <property name="mappingResources"> 
      <list> 
        <value>cn/itcast/shop/user/vo/User.hbm.xml</value> 
      </list> 
    </property> 
  </bean> 
  <!-- 事务管理: --> 
  <!-- 事务管理器 --> 
  <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager"> 
    <property name="sessionFactory" ref="sessionFactory"/> 
  </bean> 
  <!-- 开启注解事务 --> 
  <tx:annotation-driven transaction-manager="transactionManager"/> 
  <!-- Action的配置 ===========================--> 
  <!-- 首页访问的Action --> 
  <bean id="indexAction" class="cn.itcast.shop.index.action.IndexAction" scope="prototype"> 
  </bean> 
  <!-- 配置验证码Action --> 
  <bean id="checkImgAction" class="cn.itcast.shop.user.action.CheckImgAction" scope="prototype"> 
  </bean> 
  <!-- 用户模块的Action --> 
  <bean id="userAction" class="cn.itcast.shop.user.action.UserAction" scope="prototype"> 
    <!-- 注入Service --> 
    <property name="userService" ref="userService"/> 
  </bean> 
  <!-- Service的配置 ===========================--> 
  <bean id="userService" class="cn.itcast.shop.user.service.UserService"> 
    <property name="userDao" ref="userDao"/> 
  </bean> 
  <!-- Dao的配置 ===========================--> 
  <bean id="userDao" class="cn.itcast.shop.user.dao.UserDao"> 
    <property name="sessionFactory" ref="sessionFactory"/> 
  </bean> 
</beans> 
[html] view plain copy 在CODE上查看代码片派生到我的代码片
<?xml version="1.0" encoding="UTF-8" ?> 
<!DOCTYPE struts PUBLIC 
  "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN" 
  "http://struts.apache.org/dtds/struts-2.3.dtd"> 
<struts> 
  <constant name="struts.devMode" value="false" /> 
  <constant name="struts.enable.DynamicMethodInvocation" value="true"/> 
  <package name="shop" extends="struts-default" namespace="/"> 
    <global-results> 
      <result name="msg">/WEB-INF/jsp/msg.jsp</result> 
    </global-results> 
    <!-- 配置首页访问的Action --> 
    <action name="index" class="indexAction"> 
      <result name="index">/WEB-INF/jsp/index.jsp</result> 
    </action> 
    <!-- 配置用户模块的Action --> 
    <action name="user_*" class="userAction" method="{1}"> 
      <result name="registPage">/WEB-INF/jsp/regist.jsp</result> 
      <result name="input">/WEB-INF/jsp/regist.jsp</result> 
      <result name="loginPage">/WEB-INF/jsp/login.jsp</result> 
      <result name="login">/WEB-INF/jsp/login.jsp</result> 
      <result name="loginSuccess" type="redirectAction">index</result> 
      <result name="quit" type="redirectAction">index</result> 
      <result name="checkcodeFail">/WEB-INF/jsp/regist.jsp</result> 
    </action> 
    <!-- 验证码Action --> 
    <action name="checkImg" class="checkImgAction"></action> 
  </package> 
</struts> 

以上所述是小编给大家介绍的使用AJAX完成用户名是否存在异步校验,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对脚本之家网站的支持!

更多精彩内容其他人还在看

PHP+AJAX无刷新实现返回天气预报数据

天气数据是通过采集中国气象网站的。本来中国天气网站也给出了数据的API接口,接下来为大家介绍下用php来写一个天气预报的模块,感兴趣的朋友可以参考下
收藏 0 赞 0 分享

jQuery的ajax传参巧用JSON使用示例(附Json插件)

jQuery的ajax调用很方便,传参的时候喜欢用Json的数据格式,使用示例代码如下,感兴趣的朋友可以参考下,希望对大家有所帮助
收藏 0 赞 0 分享

ajax jquery 异步表单验证示例代码

异步表单验证想必大家早已如雷贯耳,本文为大家详细讲述下ajax jquery实现异步表单验证,感兴趣的朋友可以参考下
收藏 0 赞 0 分享

AJAX如何接收JSON数据示例介绍

如何使用AJAX返回JSON数据,就是dataType,当你设置json后返回的json字符串传递到客户端就是JSON对象了,示例如下,感兴趣的朋友可以参考下
收藏 0 赞 0 分享

ajax局部刷新一个div下jsp内容的方法

局部刷新某个div下的jsp可以通过setInterval或者是setTimeout来轻松实现,具体如下,有此需求的朋友可以参考下,希望对大家有所帮助
收藏 0 赞 0 分享

ajax交互Struts2的action(客户端/服务器端)

本文为大家探讨下ajax交互Struts2的action并有客户端及服务器端代码,感兴趣的朋友可以参考下,希望对大家有所帮助
收藏 0 赞 0 分享

Ajax工作原理深入理解

ajax是异步的意思,它有别于传统web开发中采用的同步的方式。异步传输是面向字符的传输,它的单位是字符;而同步传输是面向比特的传输,它的单位是桢,它传输的时候要求接受方和发送方的时钟是保持一致的
收藏 0 赞 0 分享

Ajax方式删除表格一行数据示例代码

Ajax方式删除信息在某些情况下还是蛮实用的,下面为大家具体介绍下Ajax方式如何删除表格一行数据,感兴趣的朋友可以了解下
收藏 0 赞 0 分享

Ajax叠加(Ajax返回数据用Ajax发出)示例代码

把参数用Ajax发送到数据库进行查询然后用Ajax将数据发送到数据库签到表,于是有了下面的代码,感兴趣的朋友可以了解下,希望对大家学习ajax有所帮助
收藏 0 赞 0 分享

Ajax长连接项目案例

所谓的长连接,就是不断去发送请求,把请求阻塞在服务器端,每次超过请求时间就去重新发送请求,下面以一个实例为大家详细介绍下,感兴趣的朋友可不要错过了哈
收藏 0 赞 0 分享
查看更多