Ajax Easyui Asynchronous Checksum user name _jquery

Source: Internet
Author: User
Tags uuid stringbuffer

Previously bored to write a small thing, which has a function is to add users, at that time did not consider the problem of duplication of user name, today idle to Nothing, is going to use Ajax asynchronous refresh to verify the existence of the user name. I am also a novice, just a junior, haha write the wrong place please point out.
Put on the effect chart:


First of all, preparation before preparation

I am not using native JS to write Ajax, but with the Jqueryeasyui framework of Ajax, so before using it must be introduced in jquery JS files.

 <link rel= "stylesheet" type= "Text/css" href= "${contextpath}/pages/introcontrol/util/themes/default/easyui.css" >
<link rel= "stylesheet" type= "Text/css" href= "${contextpath}/pages/introcontrol/util/themes/icon.css" >
<script type= "Text/javascript" src= "${contextpath}/pages/introcontrol/util/jquery-1.7.1.min.js" > </script>
<script type= "Text/javascript" src= "${contextpath}/pages/introcontrol/util/ Jquery.easyui.min.js "></script>
<script type=" Text/javascript "src=" ${contextpath}/pages/ Introcontrol/util/easyui-lang-zh_cn.js "></script>
<script type=" Text/javascript ">

The first is written in the JSP file

var isexist = true;//This sets an identifier to be used to block the form's Commit $ (function () {//form validation $ (' #fname '). Validatebox ({required:true, MIS
  Singmessage: ' username cannot be empty! ', precision:0};
  $ (' #floginname '). Validatebox ({required:true, missingmessage: ' Login name cannot be empty! ', precision:0});
  For the binding of the Add button $ (' #addBtn '). Bind (' click ', Function () {addUser ();

 });
});
 /** * Add User */function AddUser () {var obj = $ (' #orgTree '). Tree (' getselected ');
  if (obj) {$ (' #fdepname '). Val (' #orgTree '). Tree (' getselected '). Text);
  $ (' #fdepid '). Val ($ (' #orgTree '). Tree (' getselected '). ID);
    $ (' #operator_user '). Dialog ({width:350, height:300, title: ' Add admin ', Modal:true, buttons:[{text: ' Submit ',
        Handler:function () {//To determine whether or not to qualify if (!isexist) {if ($ (' #operator_user '). Form (' Validate ')) {$.ajax ({
        URL: "<%=request.getcontextpath ()%>/peixun/adduser.action", type: "Post", DataType: ' JSON ', data:$ (' #myform '). Serialize (), Success:functioN (data,response,status) {if (data.type== ' success ') {$.messager.alert ("Prompt", "new success!");
          $ (' #sysUserTable '). DataGrid (' reload ');
          $ (' #operator_user '). Dialog (' Close ');
         Clear the form $ (' #myform ') [0].reset (); }else{$.messager.alert ("Hint", "New failed!");}}});}}},{text: ' Fetch
     Elimination ', Handler:function () {$ (' #operator_user '). Dialog (' Close ');
    $ (' #myform ') [0].reset (); 
 }
   }],
  }); }else if (obj==null) {alert ("No Tree selected ...")
 ");
}
};  
 /** * Ajax Asynchronous Checksum user name/function checkusername () {var floginname = $ ("#floginname"). Val (); $.ajax ({url: "${contextpath}/peixun/checkusername.action", type: ' POST ', data:{loginname:floginname}, Da
   Tatype: ' JSON ', success:function (data) {//based on the data returned in the background to judge, and give hints.
    if (Data.type = = "true") {$ ("#label") [0].innerhtml=] <font color= ' red ' > sign-in name repeat </font>;
   Isexist = true;
}else if (Data.type = = "false") {    $ ("#label") [0].innerhtml= "<font color= ' green ' > Congratulations, login name can be used </font>";
   Isexist = false; }, Error:function (data) {alert ("Get user information failed, please contact admin!")
  ");


}
 });

 }

Action section

Package ais.peixun.web;
Import java.io.Serializable;
Import java.util.ArrayList;
Import Java.util.HashMap;
Import java.util.List;

Import Java.util.Map;
Import Ais.adl.model.TreeNode;
Import ais.framework.struts.BaseAction;
Import Ais.framework.util.UUID;
Import Ais.peixun.service.PeixunService;
Import Ais.user.model.UUser;

 public class Peixunaction extends Baseaction {private static final long serialversionuid = 6269156200927918770L;

 Private Peixunservice Peixunservice;
 Private map<string, object> resultmap = new hashmap<string, object> ();
 Private Uuser user;
 Private String ID;
 Private String fname;
 Private String Floginname;
 private String name;
 Private String LoginName;
 Private String Fsex;
    /** * Add User * * public String AddUser () {try{if (user!=null) {String id = new UUID (). toString ();
    User.setfuserid (ID);
    Serializable ser = this.peixunService.addOneUser (user); if (Ser!= null && ser!= "") {this.resultMap.put ("type", "SuCcess ");
    }else{this.resultMap.put ("type", "error");
  }}catch (Exception e) {e.printstacktrace ();
 return SUCCESS;
   /** * The method for the existence of the user name for the school experience * * Public String Checkusername () {try{if (loginname!=null&&loginname!=null) {
    This is done by Daoimpl the returned data to judge Int count=this.peixunservice.checkusername (loginname);
    if (count==1) {//Returns the result true in type to the foreground this.resultMap.put ("type", "true");
    }else{this.resultMap.put ("type", "false");
  }}catch (Exception e) {e.printstacktrace ();
 return SUCCESS;
 Public Peixunservice Getpeixunservice () {return peixunservice;
 public void Setpeixunservice (Peixunservice peixunservice) {this.peixunservice = Peixunservice;
 Public map<string, Object> Getresultmap () {return resultmap;
 public void Setresultmap (map<string, object> resultmap) {this.resultmap = Resultmap;
 Public String Getfname () {return fname; } public void Setfname (String fname) {this.fname = fname;
 Public String Getfloginname () {return floginname;
 } public void Setfloginname (String floginname) {this.floginname = Floginname;
 Public String Getfsex () {return fsex;
 } public void Setfsex (String fsex) {this.fsex = Fsex;
 Public String Getfdepname () {return fdepname;
 } public void Setfdepname (String fdepname) {this.fdepname = Fdepname;
 Public Uuser GetUser () {return user;
 public void SetUser (Uuser user) {this.user = user;
 Public String GetName () {return name;
 public void SetName (String name) {this.name = name;
 Public String Getloginname () {return loginname;
 } public void Setloginname (String loginname) {this.loginname = LoginName;
 Public String GetId () {return id;
 public void SetId (String id) {this.id = ID;

 }

}

Service and Serviceimpl

Package ais.peixun.service;
Import java.io.Serializable;
Import java.util.List;

Import Java.util.Map;
Import Ais.adl.model.TreeNode;
Import Ais.framework.service.IBaseService;

Import Ais.user.model.UUser; 
 Public interface Peixunservice extends Ibaseservice {public Serializable addoneuser (uuser user);

public int checkusername (String loginname);

} package Ais.peixun.service.impl; 
Import java.io.Serializable;
Import java.util.List;

Import Java.util.Map;
Import Ais.adl.model.TreeNode;
Import Ais.framework.service.impl.BaseServiceImpl;
Import Ais.peixun.dao.PeixunDao;
Import Ais.peixun.service.PeixunService;

Import Ais.user.model.UUser;

 public class Peixunserviceimpl extends Baseserviceimpl implements Peixunservice {private Peixundao Peixundao;
 Public Peixundao Getpeixundao () {return peixundao;
 public void Setpeixundao (Peixundao peixundao) {This.peixundao = Peixundao; @Override public Serializable addoneuser (uuser user) {return This.peixunDao.addOneUSer (user);
 @Override public int checkusername (String loginname) {return this.peixunDao.checkUserName (loginname);

 }

}

DAO and Daoimpl

Package Ais.peixun.dao;

Import java.io.Serializable;
Import java.util.List;
Import Java.util.Map;


Import Ais.adl.model.TreeNode;
Import Ais.framework.dao.IBaseDAO;
Import Ais.user.model.UUser;

Public interface Peixundao extends Ibasedao {public
 Serializable addoneuser (uuser user); 
 public int checkusername (String loginname);



Package Ais.peixun.dao.impl;

Import java.io.Serializable;
Import java.sql.SQLException;
Import java.util.ArrayList;
Import java.util.List;
Import Java.util.Map;
Import org.hibernate.HibernateException;
Import Org.hibernate.Query;
Import org.hibernate.Session;
Import org.springframework.dao.DataAccessException;

Import Org.springframework.orm.hibernate3.HibernateCallback;

Import Com.sybase.jdbc2.jdbc.Convert;
Import Ais.adl.model.TreeNode;
Import Ais.framework.dao.hibernate.BaseDAOImpl;
Import ais.organization.model.UOrganization;
Import Ais.peixun.dao.PeixunDao;
Import Ais.resmngt.audobj.model.AuditingObject;

Import Ais.user.model.UUser;
 /** * @author Forlangel * */public class Peixundaoimpl extends Basedaoimpl implements Peixundao {* * * Add user/*
  @Override public Serializable addoneuser (uuser user) {Serializable ser;
  try{ser = This.gethibernatetemplate (). Save (user);
   }catch (Exception e) {e.printstacktrace ();
  return null;
 Return ser; * * * AJAX checksum/@SuppressWarnings ("Unchecked ") @Override public int checkusername (String loginname) {//set an identifier to return int flag = 0;
    try{stringbuffer sbf=new stringbuffer ();
    Sbf.append ("From Uuser u where u.floginname =");
    Sbf.append ("'" +loginname+ "");
    If data is queried from the database, the user name repeats list<uuser> List =this.gethibernatetemplate (). Find (Sbf.tostring ());  
    if (list.size () > 0) {flag = 1;
   }}catch (Exception e) {e.printstacktrace ();

 return flag;
 }

}

The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.