ajax用户名验证和登录方法的整合

来源:互联网 发布:怎么给淘宝刷差评 编辑:程序博客网 时间:2024/05/22 07:55

1.定义Dao层方法

package com.tcc.dao;






import java.util.List;


import com.tcc.bean.User;


public interface usermapper {


public User list(String name);
}

2.Controller

package com.tcc.controller;


import java.util.List;


import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;




import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;


import com.tcc.bean.User;
import com.tcc.service.userservice;


@Controller
@RequestMapping("user")
public class controller {


@Autowired
private userservice us;
@ResponseBody
@RequestMapping("list")
public int list(String name){
User list = us.list(name);
if (list!=null) {
return 0;
}
else {
return 1;
}
}
}

3.jsp

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>My JSP 'index.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">    
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
<script type="text/javascript" src="${pageContext.request.contextPath}/js/jquery-1.8.3.js"></script>
<script type="text/javascript">
 $(function(){
   $("#sub").blur(function(){
      $("#sp").empty();
     if ($("#sub").val()==null||$("#sub").val()=="") {
var span=$("<span id='sp'>用户名不能为空!</span>");
$("#sub").after(span);
}
else {


$.post("${pageContext.request.contextPath}/user/list","name="+$("#sub").val(),
function(ba){
$("#ss").empty();
$("#s").empty();
if (ba==0) {
var ssr=$("<span id='ss'>用户名已被注册!</span>");
$("#sub").after(ssr);
}
else {
var ss=$("<span id='s'>用户名可以使用!</span>");
$("#sub").after(ss);
}
},"json");
}
   });
   $("#bu").click(function(){
     $.post("${pageContext.request.contextPath}/user/add",$("form").serialize(),
function(ba){
if (ba==0) {
alert("添加失败");
}
else {
alert("添加成功");
location.href="${pageContext.request.contextPath}/user/**";
}
},"json");
     
   });
 });
</script>
  </head>
  
  <body>
    <form>
    用户名:<input type="text" id="sub" name="name"><br>
    密码:<input type="password" name="pwd"><br>
    <input type="button" value="提交" id="bu">
    </form>
  </body>
</html>
原创粉丝点击