使用AJAX完成用户名是否存在异步校验:
1.事件触发:
* onblur
2.编写AJAX代码:
* 项Action中提交:传递username参数
3.编写Action
* 接收username:模型驱动接收.
4.* 编写实体类
* User
* User.hbm.xml
* 配置到spring中.
5.编写DAO
* 继承HibernateDaoSupport
* 在配置中注入sessionFactory
6.编写Service:
* 注入UserDao
* 事务管理:
核心代码实现:
function checkUsername(){
// 获得文件框值:
var username = document.getElementById("username").value;
// 1.创建异步交互对象
var xhr = createXmlHttp();
// 2.设置监听
xhr.onreadystatechange = function(){
if(xhr.readyState == 4){
if(xhr.status == 200){
document.getElementById("span1").innerHTML = xhr.responseText;
}
}
}
// 3.打开连接
xhr.open("GET","${pageContext.request.contextPath}/user_findByName.action?time="+new Date().getTime()+"&username="+username,true);
// 4.发送
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 {
// 调用Service进行查询:
User existUser = userService.findByUsername(user.getUsername());
// 获得response对象,项页面输出:
HttpServletResponse response = ServletActionContext.getResponse();
response.setContentType("text/html;charset=UTF-8");
// 判断
if (existUser != null) {
// 查询到该用户:用户名已经存在
response.getWriter().println("用户名已经存在");
} else {
// 没查询到该用户:用户名可以使用
response.getWriter().println("用户名可以使用");
}
return NONE;
}
private UserDao userDao;
public void setUserDao(UserDao userDao) {
this.userDao = userDao;
}
// 按用户名查询用户的方法:
public User findByUsername(String username){
return userDao.findByUsername(username);
}
public User findByUsername(String username){
String hql = "from User where username = ?";
List list = this.getHibernateTemplate().find(hql, username);
if(list != null && list.size() > 0){
return list.get(0);
}
return null;
}
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">
org.hibernate.dialect.MySQLDialect
true
true
false
update
cn/itcast/shop/user/vo/User.hbm.xml
[html] view plain copy 在CODE上查看代码片派生到我的代码片
/p>
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd">
/WEB-INF/jsp/msg.jsp
/WEB-INF/jsp/index.jsp
/WEB-INF/jsp/regist.jsp
/WEB-INF/jsp/regist.jsp
/WEB-INF/jsp/login.jsp
/WEB-INF/jsp/login.jsp
index
index
/WEB-INF/jsp/regist.jsp
以上所述是小编给大家介绍的使用AJAX完成用户名是否存在异步校验,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对编程圈网站的支持!