Aja检验用户名是否存在

springboot 1.4.0+jpa

判断用户表单输入的用户名是否存在,异步的查询数据库,如果用户名存在则提示"该用户已经存在",否则提示可以注册

前台页面 register.html




    
    
    
    WebJars Demo
    
    






后台代码 

package com.example.demo.test.controller;
import com.example.demo.test.Repositry.peopleRepositry;
import com.example.demo.test.entity.People;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RequestMapping("api")
@Controller
public class ajaxController {

    @Autowired
    private peopleRepositry peopleRepositry;

    @ResponseBody
    @PostMapping(value = "findUser")
    public Map people(HttpServletRequest request) {
        Map map = new HashMap();

        String username = request.getParameter("username");
        // People people =peopleRepositry.findOne(1);


        List list = peopleRepositry.findAll();
        List list1 = new ArrayList<>();
        for (People people : list) {


            list1.add(people.getUsername());

        }


        if (list1.contains(username)) {
            map.put("userExsit", true);
            map.put("msg", "老铁这个用户名太受欢迎,请更换一个");
        } else {
            map.put("userExsit", false);
            map.put("msg", "老铁这个用户名可用");
        }
        return map;
    }


}
 
  

 数据库 

Aja检验用户名是否存在_第1张图片

启动项目 测试 

Aja检验用户名是否存在_第2张图片

Aja检验用户名是否存在_第3张图片

源码地址

https://gitee.com/zdwbmw/java_basic/

你可能感兴趣的:(java)