- 要求
- 1、 前端(index.html)
- 2、后端
- 3、抓包情况
实现一个登录界面
后端:Spring Boot
前端:HTML + CSS + jQuery
前后端均使用 JSON 格式传递参数
无需下载jQuery的代码文件,只需要引用其网络链接即可
登录页面
登录界面
用户名:
密码:
2、后端
- 创建Spring Boot 项目,引入Spring Web框架、lombok框架
- 将静态页面(index.html)及它用到的相关资源,放到 resource --> static 目录下面
- 创建 Customer 普通类,接收 JSON 对象
- 在 Controller 包中,创建 Login 类(@Controller修饰,需要存储到 Spring 容器中),设置 @ResponseBody
目录结构
Customer类
package com.example.demo.model;
import lombok.Data;
@Data
public class Customer {
private String username;
private String password;
}
Login类
package com.example.demo.Controller;
import com.example.demo.model.Customer;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import java.util.HashMap;
@Controller
@ResponseBody
public class Login {
@RequestMapping("/login")
public HashMap login(@RequestBody Customer customer) {
HashMap map = new HashMap<>();
int succ = 200;
int result = 0;
String mes = "";
String username = customer.getUsername();
String password = customer.getPassword();
if (StringUtils.hasLength(username) && StringUtils.hasLength(password)) {
if (username.equals("admin") && password.equals("admin")) {
result = 1;
} else {
mes = "用户名或密码错误";
}
} else {
mes = "用户名或密码为空";
}
map.put("succ", succ);
map.put("result", result);
map.put("mes", mes);
return map;
}
}
3、抓包情况



