@RequestParam(用于字段非空校验)
作用1:实现非空校验(默认此参数必填)
作用2:实现将前端的参数指定映射到后端的某个参数上
@RequestBody(多用与对象)
作用1:参数不能为空
作用2:取值是从body里面取
@RequestMapping("/login2")
public Object login2(@RequestParam String username, HttpServletRequest request){
HashMap<String, Object> map = new HashMap<>();
int status = -1;
String msg = "未知错误";
String data = "登录失败";
if (username.equals("root")){
HttpSession session = request.getSession();
session.setAttribute("username",username);
status = 0;
msg = "";
data = "登录成功";
}else{
status = 0;
msg = "用户名输入错误";
data = "登录失败";
}
map.put("status",status);
map.put("msg",msg);
map.put("data",data);
return map;
}
@RequestMapping("/login")
public Object login( User user,HttpServletRequest request){
HashMap<String, Object> map = new HashMap<>();
int status = -1;
String msg = "未知错误";
String data = "登录失败";
if (user!=null&&user.getUsername().equals("root")&&user.getPassword().equals("root")){
HttpSession session = request.getSession();
session.setAttribute("userinfo",user);
status = 0;
msg = "";
data = "登录成功";
}else{
status = 0;
msg = "用户名错误";
data = "登录失败";
}
map.put("status",status);
map.put("msg",msg);
map.put("data",data);
return map;
}
@RequestMapping("/login3")
public Object login3(@RequestParam(name="name") String username, String password,HttpServletRequest request){
HashMap<String, Object> map = new HashMap<>();
int status = -1;
String msg = "未知错误";
String data = "登录失败";
map.put("status",status);
map.put("msg",msg);
map.put("data",username);
return map;
}
@RequestMapping("/login")
public String method3(@RequestBody(required = true) String name, String password) {
return "姓名: " + name + " , 密码:" + password;
}
@RequestMapping("/reg2")
public String reg(@RequestParam String username, @RequestParam String password, @RequestParam String img) {
return "username: " + username + ",password: " + ",img: " + img;
}
@RequestMapping("/reg3")
public String reg3( User user) {
return "username: " + user.getUsername() + ",password: " + user.getPassword() + ",img: " + user.getImg();
}