Use AJAX to verify whether the user name exists asynchronously.

Source: Internet
Author: User

Use AJAX to verify whether the user name exists asynchronously.

Use AJAX to verify whether the user name exists asynchronously:

1. event triggering:

* Onblur

2. Compile AJAX code:

* Submit in item Action: Pass the username parameter

3. Write Action

* Receiving username: model-driven receiving.

4. * compile entity classes

* User

* User. hbm. xml

* Configure it to spring.

5. Write DAO

* Inherit from HibernateDaoSupport

* Inject sessionFactory into the configuration.

6. Compile the Service:

* Inject UserDao

* Transaction management:

Core code implementation:

Function checkUsername () {// obtain the file box value: var username = document. getElementById ("username "). value; // 1. create an Asynchronous interaction object var xhr = createXmlHttp (); // 2. set the xhr listener. onreadystatechange = function () {if (xhr. readyState = 4) {if (xhr. status = 200) {document. getElementById ("span1 "). innerHTML = xhr. responseText ;}}// 3. open the connection xhr. open ("GET", "$ {pageContext. request. contextPath}/user_findByName.action? Time = "+ new Date (). getTime () + "& username =" + username, true); // 4. send 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 {// call Service to query: User existUser = userService. findByUsername (user. getUsername (); // get the response object. The output of the item Page is HttpServletResponse response = ServletActionContext. getResponse (); response. setContentType ("text/html; charset = UTF-8"); // judge if (existUser! = Null) {// query the user: the user name already has a response. getWriter (). println ("<font color = 'red'> the user name already exists </font>");} else {// This user is not found: the user name can use response. getWriter (). println ("<font color = 'green'> User name can be used </font>");} return NONE ;}
Private UserDao userDao; public void setUserDao (UserDao userDao) {this. userDao = userDao;} // User query method by User name: 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 "> <! -- Configure the connection pool: --> <! -- Introduce the External property file --> <context: property-placeholder location = "classpath: jdbc. properties"/> <! -- Configure the C3P0 connection pool: --> <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-related information --> <bean id = "sessionFactory" class = "org. springframework. orm. hibernate3.LocalSessionFactoryBean"> <! -- Inject connection pool --> <property name = "dataSource" ref = "dataSource"/> <! -- Configure other attributes of 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> <! -- Configure the Hibernate ing file --> <property name = "mappingResources"> <list> <value> cn/itcast/shop/user/vo/User. hbm. xml </value> </list> </property> </bean> <! -- Transaction Management: --> <! -- Transaction Manager --> <bean id = "transactionManager" class = "org. springframework. orm. hibernate3.HibernateTransactionManager "> <property name =" sessionFactory "ref =" sessionFactory "/> </bean> <! -- Enable annotation transaction --> <tx: annotation-driven transaction-manager = "transactionManager"/> <! -- Action configuration =================================--> <! -- Homepage access Action --> <bean id = "indexAction" class = "cn. itcast. shop. index. action. indexAction "scope =" prototype "> </bean> <! -- Configure the verification code Action --> <bean id = "checkImgAction" class = "cn. itcast. shop. user. action. CheckImgAction" scope = "prototype"> </bean> <! -- User Module Action --> <bean id = "userAction" class = "cn. itcast. shop. user. action. UserAction" scope = "prototype"> <! -- Inject Service --> <property name = "userService" ref = "userService"/> </bean> <! -- Service configuration ===========================--> <bean id = "userService" class = "cn. itcast. shop. user. service. userService "> <property name =" userDao "ref =" userDao "/> </bean> <! -- Dao configuration =============================--> <bean id = "userDao" class = "cn. itcast. shop. user. dao. userDao "> <property name =" sessionFactory "ref =" sessionFactory "/> </bean> </beans> [html] view plain copy: view the CODE piece on the CODE and derive it from me <? 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> <! -- Configure the homepage access Action --> <action name = "index" class = "indexAction"> <result name = "index">/WEB-INF/jsp/index. jsp </result> </action> <! -- Configure the user Module 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 = "redirectAct Ion "> index </result> <result name =" checkcodeFail ">/WEB-INF/jsp/regist. jsp </result> </action> <! -- Verification Code Action --> <action name = "checkImg" class = "checkImgAction"> </action> </package> </struts>

The above section describes whether asynchronous verification exists for usernames Using AJAX. I hope it will be helpful to you. If you have any questions, please leave a message and I will reply to you in a timely manner. Thank you very much for your support for the help House website!

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.