天天看點

axios-登入

1.建立首頁index.html

在 resources 目錄下 static 中 建立

<!DOCTYPE html>
<html >
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
    <h1>axios異步請求測試</h1>
    <div>        
        <a href="/login.html" target="_blank" rel="external nofollow" >登入頁面</a>
    </div>
           

2.建立 login.html

在 resources 目錄下 static 中 建立

<!DOCTYPE html>
<html >
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<div>
    <h1>登入頁面</h1>
    <input type="text" v-model="user.username" placeholder="使用者名">
    <input type="text" v-model="user.password" placeholder="密碼">
    <input type="button" value="登入" @click="login()">
</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:""
                }
            },
            methods:{
                login(){
                    axios.post("/login",v.user).then(function (response) {
                        if(response.data==1){
                            alert("登陸成功!");
                            Location.href="/" target="_blank" rel="external nofollow" ;//顯示首頁
                        }else if(response.data==2){
                            alert("使用者名不存在!");
                        }else{
                            alert("密碼錯誤!");
                        }
                    })
                }

            }
        })
    </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("/login")
    public int login(@RequestBody User user){
        User u= mapper.selectByUsername(user.getUsername());
        if(u != null){
            if(user.getPassword().equals(u.getPassword())){
                return 1;//登陸成功
            }
            return 3;//密碼錯誤
        }
        return 2;//使用者名不存在
    }
           

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);
    
}
           

6.頁面顯示

axios-登入