将Controller方法返回的对象通过适当的转换器(HttpMessageConverter)转换为指定的格式,
并将其写入到response对象得body区,一般用于返回JSON数据或者XML数据,
在使用此注解后,SpringMVC将不会再走视图处理器,而是直接将数据写入流中,效果等同于通过response对象输出指定格式的数据
绑定单个请求参数值
三个参数:
实体类Users:定义了Users类的四个属性:分别是id,name,birthday,email
package com.mvc.beans;
import com.fasterxml.jackson.annotation.JsonFormat;
import lombok.Data;
import java.util.Date;
/**
* @author Una
* @date 2022/8/13 18:55
* @description:
*/
@Data
public class Users {
private Integer id;
private String name;
//日期格式的规范
@JsonFormat(pattern = "yyyy年MM月dd日")
private Date birthday;
private String email;
public Users() {
}
public Users(Integer id, String name, Date birthday, String email) {
this.id = id;
this.name = name;
this.birthday = birthday;
this.email = email;
}
}
表单文件:一个登录界面,包含姓名、密码和提交按钮
<html>
<head>
<title>登录</title>
</head>
<style>
div{
width: 500px;
margin: 50px auto;
}
</style>
<body>
<div>
<h2>-------用户登录--------</h2>
<br/><br/>
<form action="login5" method="post">
UserName:
<input type="text" name="userName" />
<br/><br/>
PassWord:
<input type="password" name="passWord"/>
<br/><br/>
<button type="submit">Login</button>
</form>
</div>
</body>
</html>
success.html
成功界面
DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>登陆成功!!!!!title>
head>
<body>
<h1>登陆成功!!!!h1>
body>
html>
register.jsp
注册页面:包含登录名、密码、真实姓名;注册按钮
<%--
Created by IntelliJ IDEA.
User: ThinkPad
Date: 2022/8/13
Time: 19:53
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>注册页面</title>
</head>
<body>
<div style="text-align: center" >
<form action="/reg" method="post">
<table>
<tr>
<td>登录名:</td>
<td>
<label>
<input type="text" name="loginName"/>
</label>
</td>
</tr>
<tr>
<td>密码:</td>
<td>
<label>
<input type="password" name="password"/>
</label>
</td>
</tr>
<tr>
<td>真实姓名:</td>
<td>
<label>
<input type="text" name="userName"/>
</label>
</td>
</tr>
<tr>
<td>
<button type="submit">注册</button>
</td>
</tr>
</table>
</form>
</div>
</body>
</html>
login.jsp
登录界面:登录名和密码和登录按钮
<%--
Created by IntelliJ IDEA.
User: ThinkPad
Date: 2022/8/13
Time: 19:55
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>登录页面</title>
</head>
<body>
<div style="width:600px;margin: 20px auto;">
<form action="login" method="post">
<table>
<tr>
<td>登录名:</td>
<td>
<label>
<input type="text" name="loginName" />
</label>
</td>
</tr>
<tr>
<td>密码:</td>
<td>
<label>
<input type="password" name="password"/>
</label>
</td>
</tr>
<tr>
<td>
<button type="submit">登录</button>
</td>
</tr>
</table>
</form>
</div>
</body>
</html>
控制器文件:
使用@ResponseBody时;SpringMVC将不会再走视图处理器,而是直接将返回的字符串str写入流中
package com.mvc.controller;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.mvc.beans.Users;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
/**
* @author Una
* @date 2022/8/13 18:58
* @description:
*/
@Controller
public class UsersController {
@PostMapping(value = "login5", produces = "text/html; charset=utf-8")
@ResponseBody
public String getUsers() throws ParseException, JsonProcessingException {
SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
List<Users> usersList=new ArrayList<>();
Users users1=new Users(12323,"小明",sdf.parse("2002-12-15"),"22264522@qq.com");
Users users2=new Users(12322,"小红",sdf.parse("2001-12-15"),"226522@qq.com");
Users users3=new Users(12324,"小黄",sdf.parse("2008-12-15"),"2422@qq.com");
Users users4=new Users(12325,"小谢",sdf.parse("2001-12-18"),"22422@qq.com");
usersList.add(users1);
usersList.add(users2);
usersList.add(users3);
usersList.add(users4);
ObjectMapper om=new ObjectMapper();
String str=om.writeValueAsString(usersList);//格式
return str;
}
}


register方法使得填写信息后跳转到login登录界面;
loginCheck方法使得如果输入的密码和姓名有匹配,则跳转到welcome页面;否则跳转到login界面

package com.mvc.controller;
import com.mvc.beans.Admin;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import java.util.ArrayList;
import java.util.List;
/**
* @author Una
* @date 2022/8/13 19:27
* @description:
*/
@Controller
public class AdminController {
private List<Admin> adminList;
public AdminController() {
super();
this.adminList = new ArrayList<>();
}
@GetMapping(value = "/register" )
public String registerPage(){
return "register";
}
//在地址栏输入/register跳转到register.html界面
@PostMapping(value = "/reg")
public String register(@RequestParam(value = "loginName") String loginName,
@RequestParam(value = "password") String password,
@RequestParam(value = "userName") String userName){
Admin admin=new Admin();
admin.setLoginName(loginName);
admin.setPassword(password);
admin.setUserName(userName);
adminList.add(admin);
return "login";
}
@PostMapping(value = "login")
public String loginCheck(@RequestParam(value = "loginName") String loginName,
@RequestParam(value = "password") String password,
Model model){
System.out.println("登录名:"+loginName);
System.out.println("密码:"+password);
for (Admin a:adminList){
if (a.getLoginName().equals(loginName) && a.getPassword().equals(password)){
model.addAttribute("admin",a);
return "welcome";//如果有匹配,则跳转到welcome页面
}
}
return "login";//如果没有匹配,则跳转到login页面
}
}
在地址栏输入register跳转到register.jsp页面:进行注册

注册完毕按注册按钮;跳转到login.jsp页面进行登录;

匹配成功跳转到success.html页面
