0%

newcoder项目前置知识之SpringMVC

官方文档:Servlet 栈的 Web 应用 (springdoc.cn)

SpringMVC三层架构

Spring是分为三层:表现层、业务层、数据层

SpringMVC:服务于表现层,将表现层分为controller(控制层)、model(模型层)、view(视图层)

model层已有,需要自己写的是controller和模板引擎(thymeleaf

请求响应数据

1.响应String类型数据

将方法的返回值直接作为HTTP响应的内容返回

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
// 只有GET方法能请求到
// /students?current=1&limit=20
@RequestMapping(value = "/students", method = RequestMethod.GET)
@ResponseBody
public String getStudents(
@RequestParam(name = "current", required = false, defaultValue = "1") int current,
@RequestParam(name = "limit", required = false, defaultValue = "10") int limit) {
System.out.println(current);
System.out.println(limit);
return "some students";
}

// /student/123, 获取参数的方式
@RequestMapping(value = "/student/{id}", method = RequestMethod.GET)
@ResponseBody
public String getStudent(@PathVariable(name = "id")int id) {
System.out.println(id);
return "a student";
}

// POST请求
@RequestMapping(value = "/student", method = RequestMethod.POST)
@ResponseBody
public String saveStudent(String name, int age) {
System.out.println(name);
System.out.println(age);
return "success";
}

2. 响应HTML数据

SpringMVC会根据视图解析器将逻辑视图名称解析成实际的视图文件路径,然后将模型中的数据渲染到该视图文件中。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
   // 响应HTML数据
@RequestMapping(value = "/teacher", method = RequestMethod.GET)
public ModelAndView getTeacher() {
ModelAndView mav = new ModelAndView();
mav.addObject("name", "gxy");
mav.addObject("age", "22");
mav.setViewName("/demo/view");
return mav;
}
// 更为简洁的方法
@RequestMapping(value = "/school", method = RequestMethod.GET)
public String getSchool(Model model) {
model.addAttribute("name", "ouc");
model.addAttribute("age", "99");
return "/demo/view";
}

视图文件使用thymeleaf模板引擎,用于在HTML页面上生成动态数据

1
2
3
4
5
6
7
8
9
10
11
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Teacher</title>
</head>
<body>
<p th:text="${name}"></p>
<p th:text="${age}"></p>
</body>
</html>

3. 响应JSON数据

一般用于异步请求中,异步请求通俗说就是不刷新页面,但访问了服务器

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
// 相应JSON数据(一般在异步请求中)
// 通过JSON字符串,JAVA对象可以转成JS对象
@RequestMapping(value = "/emp", method = RequestMethod.GET)
@ResponseBody
public Map<String, Object> getEmp() {
Map<String, Object> emp = new HashMap<>();
emp.put("name", "gxy");
emp.put("age", 23);
emp.put("salary", "15k");
return emp;
}

@RequestMapping(value = "/emps", method = RequestMethod.GET)
@ResponseBody
public List<Map<String, Object>> getEmps() {
List<Map<String, Object>> list = new ArrayList<>();
Map<String, Object> emp = new HashMap<>();
emp.put("name", "gxy");
emp.put("age", 23);
emp.put("salary", "15k");
list.add(emp);

emp.put("name", "hyy");
emp.put("age", 19);
emp.put("salary", "15k");
list.add(emp);
return list;
}

4. 响应图片数据

返回一个图片,示例为响应验证码图片

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
@RequestMapping(path = "/kaptcha", method = RequestMethod.GET)
public void getKaptcha(HttpServletResponse response, HttpSession session) {
String text = kaptchaProducer.createText();
BufferedImage image = kaptchaProducer.createImage(text);
// 将验证码返回给session
session.setAttribute("kaptcha", text);
// 设置响应类型
response.setContentType("image/png");
// 将图片输出给浏览器(将生成的图片写入响应数据流)
try {
OutputStream os = response.getOutputStream();
ImageIO.write(image, "png", os);
} catch (IOException e) {
logger.error("响应验证码失败" + e.getMessage());
}
}