官方文档: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
|
@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"; }
@RequestMapping(value = "/student/{id}", method = RequestMethod.GET) @ResponseBody public String getStudent(@PathVariable(name = "id")int id) { System.out.println(id); return "a student"; }
@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
| @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
|
@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.setAttribute("kaptcha", text); response.setContentType("image/png"); try { OutputStream os = response.getOutputStream(); ImageIO.write(image, "png", os); } catch (IOException e) { logger.error("响应验证码失败" + e.getMessage()); } }
|