Use Struts2+ajax+jquery to verify that the user name has been registered _jquery

Source: Internet
Author: User

Recommended Reading : Jquery+ajax+struts2+hibernate Framework Integration to achieve full login registration

In the user module of the user registration requirements, usually to the user name has been registered verification, today just write this demand, the detailed code and the problems encountered. When using Struts2+ajax, we usually return JSON-type data, but like the example above, We just want to return a 1 and a 0 to help with the subsequent judgment, it's not necessary to return the JSON type and return a text string.

Regist.jsp (for <script> only):

<script type= "Text/javascript" >
$ (function () {
$ ("#userNiName"). blur (function () {
var val = $ (This ). Val ();
val = $.trim (val);
var $this = $ (this);
if (Val!= "") {
//delete all the font sibling nodes after the current node
$this. Nextall ("Font"). Remove ();
var url = "User_checkname";
var args = {
"Userniname": Val,
"Time": New Date ()
};
$.post (URL, args, function (data) {
//indicates the available
if (data = = "1") {
$this. After ("<font color= ' green ' > User name available!</font> ");
}
Cannot use
else if (data = = "0") {
$this. After ("<font color= ' Red ' > username has been registered!</font>");
}
Server error
else {
alert ("Server Error!");}}
);
} else {
$ (this). Val ("");
$this. focus ();}}
)

Here I used the Jquery.post (url,args,function (data) {.}) The function submits the AJAX request to the specified URL, carries the parameter args, and finally uses a callback function to process the request to return the result data.

Useraction:

public class Useraction extends Actionsupport implements modeldriven<user>{
private user user=new user ();
Private UserService UserService;
Private InputStream InputStream;
Public InputStream getInputStream () {return
inputstream;
}
Verify that the user nickname exists public
String CheckName () throws unsupportedencodingexception{
System.out.println ("Enter Ajax test") ;
String userniname=user.getuserniname ();
if (Userservice.finduserbyname (userniname) ==null) {
inputstream=new bytearrayinputstream ("1". GetBytes ("UTF-8 "));
} else{
inputstream=new Bytearrayinputstream ("0". GetBytes ("UTF-8"));
return "AJAX_SUCC";
}
@Override public
User Getmodel () {return
user;
}
public void Setuserservice (UserService userservice) {
this.userservice = userservice;
}
} 

Note: Here I use the Modeldriven way to get form data, and in the use of Ajax, the direct transmission of a userniname (nickname), so I added a private string variable in Useraction userniname, The Set method is set. The result is not worth, passed over is a null, so I delete the private variable, and then directly with User.getuserniname () directly obtained. Remember.

UserService (omitted).

User class (omitted).

Userdao:

Package Com.wang.shop.user.dao;
Import java.util.List;

Import Org.springframework.orm.hibernate4.support.HibernateDaoSupport;
Import Com.wang.shop.user.entity.User;
public class Userdao extends hibernatedaosupport{
/**
* Query user's nickname
* @param userniname
* @return
*/Public
User finduserbyname (String userniname) {
list<user> List = (list<user>) This.gethibernatetemplate (). Find ("Select u from User u where u.userniname=?", userniname);
if (List!=null&&list.size () >0) {
System.out.println ("list:" +list.get (0));
Return List.get (0);
}
return null;
}
}

Struts.xml:

<?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>
<package name=" shop "namespace="/"extends=" Struts-default ">
<!--useraction--> <action name=" user_* "class=
" useraction "method=" {1} ">
<result name= "REGIST_SUCC" >/WEB-INF/jspForUser/login.jsp</result>
<result type= "Stream" Name= "AJAX_SUCC" >
<param name= "ContentType" >text/html</param>
<param name= " InputStream ">inputStream</param>
</result>
</action>
</package>

Note that in the result label, type= "stream".

At last, incidentally, I used to use output statements when I was debugging, but every time I added an output statement, I needed to restart Tomcat. So refer to the online way, will tmocat installation directory, conf->server.xml in the

<!--Docbase is the full path to your project, path is to write your project name, reloadable= "true", you can automatically reload the modified class file-->

It is important to note that when you uninstall the item in tomcat, be sure to come back and remove the label.

Use Struts2+ajax+jquery to verify that the user name has been registered knowledge, small series to introduce so many people, I hope to help you!

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.