1.大体结构
Spring MVC:
- 表现层、业务层、数据访问层
MVC(解决的是表现层的问题与springMVC有很大差别):
- Model:模型层(数据)
- View:视图层
- Controller:控制层
核心组件:
- 前端控制器:DispatcherServlet
1.2 Spring MVC 架构图
注意:后续会手动画一个完整的SpringMVC流程图
Thymeleaf(模板引擎)
模板引擎
- 生成动态的HTML
Thymeleaf
- 倡导自然模板,即以HTML文件为模板
常用语法
- 标准表达式、判断与循环、模板的布局
官网:https://www.thymeleaf.org
2 代码实战
pom.xml
<!--thymeleaf--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> <version>2.7.3</version> </dependency>
控制器代码
package top.chenxiky.community.controller;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.*;
/**
* 测试控制器
*
* @author chenxiky
* @version 1.0.0
* @since 2022/10/03/12:32
*/
@RequestMapping("public/v1/test")
@Controller
public class AlphaController {
@GetMapping("http")
public void http(HttpServletRequest request, HttpServletResponse response) {
// 获取请求的数据
System.out.println(request.getMethod());
System.out.println(request.getServletPath());
Enumeration<String> headerNames = request.getHeaderNames();
while (headerNames.hasMoreElements()) {
String name = headerNames.nextElement();
String header = request.getHeader(name);
System.out.println(name + ":" + header);
}
// 获取请求值
System.out.println(request.getParameter("code"));
// 返回响应数据
response.setContentType("text/html;charset=utf-8");
try (
PrintWriter writer = response.getWriter()
){
writer.write("<h>牛客网</h>");
} catch (IOException e) {
e.printStackTrace();
}
}
// GET 请求 获取数据
@RequestMapping(path = "/student",method = RequestMethod.GET )
@ResponseBody
public String getStudents(
// 请求的参数@RequestParam(name = "current")
@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 getByIdInfo(@PathVariable("id") int id) {
System.out.println(id);
return "a" + id + "student";
}
// post请求
@RequestMapping(path = "/students",method = RequestMethod.POST )
@ResponseBody
public String saveStudent(String name, int age) {
System.out.println(name);
System.out.println(age);
return "save";
}
// 返回html数据
@RequestMapping(path = "/teacher",method = RequestMethod.GET )
public ModelAndView getTeacher() {
ModelAndView modelAndView = new ModelAndView();
modelAndView.addObject("name","张三");
modelAndView.addObject("age",30);
modelAndView.setViewName("/demo/view");
return modelAndView;
}
// 返回html
@RequestMapping(path = "/school",method = RequestMethod.GET )
public String getSchool(Model model) {
model.addAttribute("name","北京大学");
model.addAttribute("age",80);
return "/demo/view";
}
// 返回一个map
@GetMapping("/emp")
@ResponseBody
public Map<String,Object> emp() {
HashMap<String, Object> map = new HashMap<>();
map.put("name","张三");
map.put("salary",8000.0);
return map;
}
@GetMapping("/empList")
@ResponseBody
public List<Map<String, Object>> empList() {
List<Map<String, Object>> list = new ArrayList<>();
HashMap<String, Object> map = new HashMap<>();
map.put("name","张三");
map.put("salary",8000.0);
list.add(map);
map = new HashMap<>();
map.put("name","李四");
map.put("salary",8000.0);
list.add(map);
return list;
}
}
前端代码
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<p th:text="${name}"></p>
<p th:text="${age}"></p>
</body>
</html>
待完待续....