Implementation of jquery Ajax in Struts2+spring2+hibernate3 framework
Source: Internet
Author: User
Recently in a project, is to write while learning some things. Put the frame of the struts2+spring2+hibernate3 to run through, encountered to do user name verification, think of using Ajax to do. Used to write their own handwriting, this time to try to do with components, the Internet looked at the mainstream of the main AJAX components are ext, DWR, jquery. Try to use DWR, feel quite simple, but after asking a colleague, said Dwr is just AJAX components, and is said to have encountered a bug, recommended me to use jquery. Look at the online jquery things, although to subvert some of their usual JS coding habits, but this thing is really good, study a night finally put jquery Ajax in the framework of the MVC to match up, not easy Ah, oh, the following is the relevant code (write only the key parts, Need complete source can e-mail to me)
register.jsp pages used for registration are initiated by this page Ajax is also the most critical page
Java code $ (document). Ready (function () { //when UserID control loses focus $ ("#stUserId"). blur (function () { var stuserid=$ ("#stUserId"); // Start jquery's ajax $.ajax ({ //ajax by Get Way type: "Get", //ajax's address Here I'm using a action configuration please see the following struts configuration url: "Ajaxuserid", //values sent to Ajax stored in Key:value mode Final stitching to the back of the URL &nBsp; data: {stuserid:stuserid.val ()}, //Here's a key place Whether to commit using the data in the cache, default to true // If True if you have previously submitted stuserid=test, then when When you //your stuserid=test, you won't be able to trigger Ajax. cache: false, // data is the value returned when the call succeeds textstatus is a state (success or failure) success: function (Data, textStatus) { alert ( Data); }  &NBSP}); }); });
After Ajax, you'll find the action through struts.
Struts.xml
The name of the Java code//action is the Ajax URL in the previous page//to find the registaction bean in spring and call it in the Java class Ajaxuserid () method <action name= "AJ Axuserid "class=" registaction method= "Ajaxuserid" >//Send the return value to the following page <result>/sysuser/ajaxuser.jsp</r Esult> </action>
Spring.xml
Java code//equals to find the Ajaxuserid () method in the Sysuserbasicimpl class <bean id= "Registaction" Com.myweb.action.SysUserBasicImpl "scope=" prototype "> <property name=" Sysuserbasicdao "> <ref l Ocal= "Sysuserbasicdao"/> </property> </bean>
Sysuserbasicimpl.java
Java code //used to get AJAX-passed Stuserid parameters private string stuserid; // The return value is a Boolean value shows whether the userid private boolean isexist; // Allow ajaxuser.jsp page to get return value isexist public boolean getisexist () { return isExist; } //Let spring inject Stuserid value in public Void setstuserid (String stuserid) { this.stuserid = stUserId; } //ajax Call method Public string ajaxuserid ( ) { list list=sysuserbasicdao.gethibernatetemplate (). Find (" from sysuserbasic where st_user_id=? ", Stuserid); isexist = (List.size () >0); return SUCCESS; }
Ajaxuser.jsp is used to get the return value
Java code///Use struts tag <%@ taglib uri= "/struts-tags" prefix= "s"%>///return value output let Ajax emit page can get the value of this page <s:propert Y value= "isexist"/>
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.