问答平台(1),SpringMVC入门

HTTP

1
2
3
- HyperText Transfer Protocol 
- 用于传输 HTML 等内容的应用层协议
- 规定了浏览器和服务器之间如何通信,以及通信时的数据格式。

Spring MVC

1
2
3
4
5
6
7
8
9
- 三层架构
- 表现层、业务层、数据访问层
- 分层目的:解耦
- MVC
- Model:模型层
- View:视图层
- Controller:控制层
- 核心组件
- 前端控制器:DispatcherServlet

示意图

三层架构-图示

Thymeleaf

1
2
3
4
5
6
- 模板引擎
- 生成动态的 HTML。
- Thymeleaf
- 倡导自然模板,即以 HTML 文件为模板。
- 常用语法
- 标准表达式、判断与循环、模板的布局。

配置

  • application-develop.properties
    1
    2
    # ThymeleafProperties
    spring.thymeleaf.cache=false

示意图

模板引擎-图示

示例

理解 MVC: 处理请求与响应

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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
// AlphaController.java
@Controller
@RequestMapping("/alpha")
public class AlphaController {

@Autowired
private AlphaService alphaService;

@RequestMapping("/http")
public void http(HttpServletRequest request, HttpServletResponse response) {
// 获取请求数据
System.out.println(request.getMethod());
System.out.println(request.getServletPath());
Enumeration<String> enumeration = request.getHeaderNames();
while (enumeration.hasMoreElements()) {
String name = enumeration.nextElement();
String value = request.getHeader(name);
System.out.println(name + ":" + value);
}
System.out.println(request.getParameter("code"));

// 返回响应数据
response.setContentType("text/html;charset=utf-8");
try (
PrintWriter writer = response.getWriter();
) {
writer.write("<h1>牛客网</h1>");
} catch (IOException e) {
e.printStackTrace();
}
}

// GET请求
// /students?current=1&limit=20
@RequestMapping(path = "/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(path = "/student/{id}", method = RequestMethod.GET)
@ResponseBody
public String getStudent(@PathVariable("id") int id) {
System.out.println(id);
return "a student";
}

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

// 响应HTML数据
@RequestMapping(path = "/teacher", method = RequestMethod.GET)
public ModelAndView getTeacher() {
ModelAndView mav = new ModelAndView();
mav.addObject("name", "字节跳动");
mav.addObject("age", 8);
mav.setViewName("/demo/view");
return mav;
}

@RequestMapping(path = "/school", method = RequestMethod.GET)
public String getSchool(Model model) {
model.addAttribute("name", "北京大学");
model.addAttribute("age", 100);
return "/demo/view";
}

// 响应JSON数据(异步请求)
// Java对象 -> JSON字符串 -> JS对象(等其他语言的对象)
@RequestMapping(path = "/emp", method = RequestMethod.GET)
@ResponseBody
public Map<String, Object> getEmp() {
Map<String, Object> emp = new HashMap<>();
emp.put("name", "李成风");
emp.put("age", 23);
emp.put("salary", 8000.00);
return emp;
}

@RequestMapping(path = "/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", "李成风");
emp.put("age", 23);
emp.put("salary", 8000.00);
list.add(emp);

emp = new HashMap<>();
emp.put("name", "刘毅");
emp.put("age", 22);
emp.put("salary", 9000.00);
list.add(emp);

emp = new HashMap<>();
emp.put("name", "章七");
emp.put("age", 22);
emp.put("salary", 7000.00);
list.add(emp);

return list;
}
}

参考资料


问答平台(1),SpringMVC入门
https://lcf163.github.io/2020/04/12/问答平台(1),SpringMVC入门/
作者
乘风的小站
发布于
2020年4月12日
许可协议