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

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>

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值