Home >Web Front-end >JS Tutorial >Use struts2+Ajax+jquery to verify whether the username has been registered_jquery

Use struts2+Ajax+jquery to verify whether the username has been registered_jquery

WBOY
WBOYOriginal
2016-05-16 15:10:021118browse

Recommended reading: JQuery+Ajax+Struts2+Hibernate framework integration to achieve complete login registration

Regarding the user registration requirements in the user module, it is usually necessary to verify whether the user name has been registered. I just wrote this requirement today and posted the detailed code and problems encountered. When using struts2+ajax , usually we will return json type data, but like the above example, we only hope to return a 1 and 0 to facilitate subsequent judgment. There is no need to return the json type, just return a text string.

regist.jsp (only the 3f1c4e4b6b16bbbd69b2ee476dc4f83a part is provided here):

<script type="text/javascript">
$(function() {
$("#userNiName").blur(function() {
var val = $(this).val();
val = $.trim(val);
var $this = $(this);
if (val != "") {
//把当前节点后面的所有 font 兄弟节点删除
$this.nextAll("font").remove();
var url = "user_checkName";
var args = {
"userNiName" : val,
"time" : new Date()
};
$.post(url, args, function(data) {
//表示可用
if (data == "1") {
$this.after("<font color='green'>用户名可用!</font>");
}
//不可用
else if (data == "0") {
$this.after("<font color='red'>用户名已被注册!</font>");
}
//服务器错误
else {
alert("服务器错误!");
}
});
} else {
$(this).val("");
$this.focus();
}
});
})
</script> 

Here I use the Jquery.post(url,args,function(data){..}) function to submit an ajax request to the specified url, and carry the parameters args, and finally use a callback function to process the request and 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;
}
//检验用户昵称是否存在
public String checkName() throws UnsupportedEncodingException{
System.out.println("进入ajax检验");
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 method to obtain form data, and when using ajax, a userNiName (nickname) is directly passed over, so I added a private string variable userNiName in UserAction and set set method. As a result, the value could not be passed, and a null was passed, so I deleted the private variable and then used user.getUserNiName() to get it directly. 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{
/**
* 通过用户昵称查询User
* @param userNiName
* @return
*/
public User findUserByName(String userNiName){
List<User> list = (List<User>) this.getHibernateTemplate().find("select u from User u where u.userNiName=&#63;", userNiName);
if(list!=null&&list.size()>0){
System.out.println("list:"+list.get(0));
return list.get(0);
}
return null;
}
}

struts.xml:

<&#63;xml version="1.0" encoding="UTF-8" &#63;>
<!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>
</struts> 

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

Finally, by the way, I am used to using output statements when debugging, but every time I add an output statement, I need to restart Tomcat, which is very annoying. So I referred to the online method and put conf->server in the Tmocat installation directory. By adding the following code under the f7e6dec31ab1a0471d06c55afaca8d77 tag in .xml, you can modify the class (minor modifications) without restarting the server:

<!--docBase就是你的项目工程所在的全路径,path就是写你的项目名,reloadable="true",可以自动重新加载修改过的class文件-->
<Context debug="0" docBase="D:\Tomcat 7.0\webapps\SSH_shop01" path="/SSH_shop01" reloadable="true" /> 

It is important to note that after uninstalling the project in Tomcat, you must come back and delete this label.

The editor will introduce you to the knowledge of using struts2+Ajax+jquery to verify whether the user name has been registered. I hope it will be helpful to you!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn