axios-注册

1.新建首页index.html

在 resources 目录下 static 中 新建

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
    <h1>axios异步请求测试</h1>
    <div>        
        <a href="/reg.html">注册页面</a>
    </div>

2.新建 reg.html

在 resources 目录下 static 中 新建

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<div>
    <h1>注册页面</h1>
    <!--@blur代表失去焦点事件-->
    <input type="text" v-model="user.username" placeholder="请输入用户名" @blur="f()">{{info}}<br>
    <input type="text" v-model="user.password" placeholder="请输入密码"><br>
    <input type="text" v-model="user.nick" placeholder="请输入昵称"><br>
    <!--v-bind:disabled="true" 不能点-->
    <input type="button" value="注册" @click="reg()" v-bind:disabled="info == '用户名已存在'">
</div>
<script src="https://cdn.bootcdn.net/ajax/libs/axios/0.21.1/axios.min.js"></script>
<script src="https://cdn.staticfile.org/vue/2.2.2/vue.min.js"></script>
    <script>
        let v = new Vue({
            el:"body>div",
            data:{
                user:{
                    username:"",
                    password:"",
                    nick:""
                },
                info:""
            },
            methods:{
                reg(){
                    axios.post("/reg",v.user).then(function (response) {
                        if (response.data==1) {
                            alert("注册成功!")
                            //修改浏览器地址
                            location.href="/";//显示到首页
                        }else{
                            alert("用户名已存在!")
                        }
                    })
                },
                f(){
                    console.log("编辑完成!");
                    axios.get("/check?username="+v.user.username).then(function (response) {
                        v.info = response.data==1?"用户名可用!":"用户名已存在";
                    })
                }
            }
        })
    </script>
</body>
</html>

3.新建User实体类

新建entity包下创建User类 

也可使用lombok注解生成get,set,toString,这里懒得改了,

package cn.detu.boot07.entity;

public class User {
    private Integer id;
    private String username;
    private String password;
    private String nick;

    @Override
    public String toString() {
        return "User{" +
                "id=" + id +
                ", username='" + username + '\'' +
                ", password='" + password + '\'' +
                ", nick='" + nick + '\'' +
                '}';
    }

    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 String getNick() {
        return nick;
    }

    public void setNick(String nick) {
        this.nick = nick;
    }
}

4.新建UserController

controller 包下新建 控制层
package cn.detu.boot07.controller;

import cn.detu.boot07.entity.User;
import cn.detu.boot07.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class UserController {
    @Autowired(required = false)
    UserMapper mapper;

    @RequestMapping("/check")
    public int check(String username){
        User u= mapper.selectByUsername(username);
        if(u != null){
            return 2;//代表用户已存在
        }
        return 1;
    }

    @RequestMapping("reg")
    public int reg(@RequestBody User user){
        System.out.println("user = " + user);
        User u = mapper.selectByUsername(user.getUsername());
        if (u!=null){
            return 2;//代表用户名已存在
        }
        mapper.insert(user);
        return 1;
    }
}

5..新建UserMapper

mapper包下新建

package cn.detu.boot07.mapper;

import cn.detu.boot07.entity.User;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;

@Mapper
public interface UserMapper {
    @Select("select password from user where username = #{username}")
    User selectByUsername(String username);
    @Insert("insert into user values(null,#{username},#{password},#{nick})")
    void insert(User user);
}

6.页面显示

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值