标签:用户名存在与否校验 mybatis struts ajax json
操作系统:Windows 7
应用程序服务器:apache-tomcat-6.0.18
开发工具:MyEclipse8.6
Java版本:JDK1.6
MyBatis: 3.2.2
Struts版本:struts-2.3
jQuery版本:jquery-1.4.2.js
MySql版本: 5.0 MySQL Community Server (GPL)
mysql-connector-java:5.0.18
2013年4月16日注:本例中采用的JSON数据格式,推荐使用性能更好的jackson进行序列化,不要使用json-lib
关键点:
1.引入包:struts2对数据进行json序列化,必须要用到struts2的json-lib,struts2-json-plugin包;
2.json数据处理:异步提交的数据采用json格式,struts2-json-plugin会对数据进行json处理,所以struts2配置文件struts.xml中的package节点的extends必须是:extends="json-default";
例如:<package name="default" namespace="/test" extends="json-default">
3.同样的由于返回的是json数据,所以result的类型也必须采用json;
注意:struts2只能在引入json-lib,struts2-json-plugin包时,result的type属性设定json才不会报错:<result name="userInfo" type="json"></result>
4.针对异步提交,action的result节点值应该为空,即不能再转向(例如:只能为<result name="userInfo" type="json"></result>);
时序:
绿色代表客户端请求;紫色代表从数据库返回到客户端。
jsp-----(表单提交)---->jQuery------(ajax异步)----->Struts2-----(action取得json数据)----->调用service------->其它(mybatis等)-----(model)----->DB
DB----(model)------>mybatis--------->service-------Struts2(action,result)------->jQuery(ajax)---------->jsp
数据表结构
领域模型
package qh.zcy.entity; import java.util.Date; public class UserInfo { private Integer id; private String username; private String password; private Integer age; private Date insertdata; public UserInfo(Integer id, String username, String password, Integer age, Date insertdata) { super(); this.id = id; this.username = username; this.password = password; this.age = age; this.insertdata = insertdata; } public UserInfo() { super(); } @Override public String toString() { // TODO Auto-generated method stub return "id="+id+",username="+username+",password="+password+",age="+age+",insertdata="+insertdata; } public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } public Date getInsertdata() { return insertdata; } public void setInsertdata(Date insertdata) { this.insertdata = insertdata; } }
底层BaseDaoImpl.java
public int isIn(Map<String, String> isInMap){ SqlSession sqlSession=GetSqlSession.getSqlSession(); int count=sqlSession.selectOne("qh.zcy.dao.UserInfo.isIn", isInMap); return count; }UserInfoDaoImpl.java
public class UserInfoDaoImpl implements UserInfoDao { private BaseDao dao=GetBaseDao.getBaseDao(); @Override public int isIn(Map<String, String> isInMap) { // TODO Auto-generated method stub int count=-1; try { count=dao.isIn(isInMap); } catch (Exception e) { // TODO: handle exception e.printStackTrace(); GetSqlSession.rollback(); }finally{ GetSqlSession.commit(); } return count; } }
public class UserInfoServiceImpl implements UserInfoService { private UserInfoDao dao=new UserInfoDaoImpl(); @Override public boolean isIn(Map<String, String> isInMap) { // TODO Auto-generated method stub boolean flag=false; int count =dao.isIn(isInMap); Log.getLog().info("count="+count); if(count >0){ flag=false; } else{ flag=true; } return flag; } }
public class IsInAction extends ActionSupport{ private String msg; private String username; public String getMsg() { return msg; } public void setMsg(String msg) { this.msg = msg; } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } @Override public String execute() throws Exception { // TODO Auto-generated method stub UserInfoService service=new UserInfoServiceImpl(); Log.getLog().info("username="+username); Map<String, String> isInMap=new HashMap<String, String>(); isInMap.put("username", username); @SuppressWarnings("unused") boolean flag=service.isIn(isInMap); Log.getLog().info("flag="+flag); if(flag==false){ this.setMsg("该用户名已存在"); }else{ this.setMsg("你可以使用这个用户名"); } return SUCCESS; } }
<?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.action.extension" value="action"></constant> <constant name="struts.devMode" value="true"></constant> <package name="JSONName" namespace="/" extends="json-default"> <action name="isIn" class="qh.zcy.action.IsInAction"> <result type="json" name="success"></result> </action> </package> </struts>表现层 json ajax jquery
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title>注册UserInfo</title> <script type="text/javascript" src="js/jquery-1.4.2.js"></script> <script type="text/javascript"> $(function(){ $("#username").mouseleave(function(){ var username=$("#username").val(); //alert("username="+username); if(username==""){ $("#msg").html("用户名不为空"); return ; } $.ajax({ url:'isIn.action', type:'POST', data:"username="+username, dataType:'json', success:function (data) { $("#msg").html(data.msg); } }); $("#username").keydown(function(){ $("#msg").html(null); }); }); }); </script> </head> <body> <form action="ajax.do" method="post"> <p>用户名:<input type="text" name="username" id="username"/><div id="msg"></div> </p> <p>密码:<input type="password" name="password" id="password"/></p> <p><input type="button" name="login" id="login" value="登陆"/></p> </form> </body> </html>
标签:用户名存在与否校验 mybatis struts ajax json
原文地址:http://blog.csdn.net/zhanglu1236789/article/details/45012387