spring mvc+spring+mybatis+ajax实现登录验证

Spring Mvc+Spring+Mybatis+Ajax 实现异步登录的例子,和大家分享一下。

login.js代码:
$(document).ready(function(){
      $("#tname").blur(function(){
    	
         $.ajax({
        	 type:"post",
        	 url:"checklogin.do",
        	 data:{tname:$("#tname").val()},
        	 dataType:"text",
        	 beforeSend:function(){
        		 $("#btn").val("正在提交,请稍等...");
        		 }, 
        	 success:function(data){
                 //判断输入是否成功,成功则跳转   
        		 if("用户名可用!"==data){
        			 $("#tn").addClass("tn");
        			 $("#btn").val("Login");
        			
        		 }else{
        			 $("#tn").removeClass("tn").addClass("tnx");
        			 $("#btn").val("Login"); 
        			 
        			 return false;
        		 }        		           
            }   
         });
 		
      });
      $('#btn').click(function(){
    	  if($("#tname").val()=="请输入用户名" || $("#tname").val()==""||$("#password").val()=="请输入密码"|| $("#password").val()==""){
    		  $("#msg").html("用户名不能为空!");
 			 return false;
 		 }else{
 			$.ajax({
 	        	 type:"post",
 	        	 url:"login.do",
 	        	 data:{tname:$("#tname").val(),tpwd:$("#password").val()},
 	        	 dataType:"text",
 	        	 beforeSend:function(){
 	        		 $("#btn").val("正在提交,请稍等...");
 	        	 }, 
 	        	 success:function(data){
 	                 //判断输入是否成功,成功则跳转   
 	        		 if("用户名可用!"==data){
 	        			 window.location.href="index.jsp";	 
 	        		 }else{
 	        			 $("#key").addClass("key");
 	        			$("#btn").val("Login");
 	        			 return false;
 	        		 }        		           
 	            }   
 	         });
 		 }
      })
  });

login.java代码:

package com.bky.controller;
 
 
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
 
 
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
 
 
import org.jboss.weld.servlet.ServletApiAbstraction;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
 
 
import com.bky.model.Add;
import com.bky.service.BaseService;
 
 
@Controller
public class Login {
	@Autowired
	BaseService baseService;
	@RequestMapping("checklogin")	
	public String checkLogin(Add adds,HttpServletRequest request, HttpServletResponse response,Model model) throws IOException {      
		//获取页面传过来的账号
		String tname=adds.getTname();
		adds.setTname(tname);
		String mes="用户名可用!";
		Add add=baseService.selectByName(adds);
		//判断用户名是否正确,正确则登录,错误则提示重新输入
		if(add!=null){
			response.setContentType("text/plain;charset=UTF-8");
			response.getWriter().write(mes);
		}else{
			mes="用户名不正确!";
			//设置字符集
			response.setContentType("text/plain;charset=UTF-8");
			response.getWriter().write(mes);
		}
		return null;
	 }
	@RequestMapping("login")	
	public String Login(Add adds,HttpServletRequest request, HttpServletResponse response,Model model) throws IOException {
		  
	     //获取页面传过来的账号和密码
		String tname=adds.getTname();
		String tpwd=adds.getTpwd();
		adds.setTname(tname);
		adds.setTpwd(tpwd);
		String mes="用户名可用!";
		Add add=baseService.selectByNP(adds);
		 //判断用户名及密码是否正确,正确则登录,错误则提示重新输入
		if(add!=null){
			//设置字符集
			response.setContentType("text/plain;charset=UTF-8");
			response.getWriter().write(mes);
		}else{
			mes="用户名或密码不正确!";
			response.setContentType("text/plain;charset=UTF-8");
			response.getWriter().write(mes);
		}
		return null;
	 }
}

login.jsp代码:



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

登录

Copyright © 2015.Company name All rights reserved.

 

参考文章:

https://blog.csdn.net/yikong2yuxuan/article/details/50402725

https://blog.csdn.net/yanglong_blog_/article/details/78480192

 

 

你可能感兴趣的:(jsp)