HTTP
超文本传输协议,用于传输 HTML 等内容的应用层协议,
规定了浏览器和服务器之间如何通信,以及通信时的数据格式。
三层架构和MVC
三层架构:表现层、业务层、数据层
MVC:Model 模型层(数据)、View 视图层、Controller 控制层
浏览器请求先到 Controller,Controller 让业务层处理,得到数据封装到 Model 中,然后传给 View,View 生成 HTML 返回给浏览器。
前端控制器 DispatcherServlet 负责调度 MVC(下图 Front controller 指 DispatcherServlet)
Thymeleaf
模板引擎:生成动态的 HTML
Thymeleaf 优势在于:倡导自然模板,以 HTML 文件为模板
相关操作
常用注解
application.properties
# 默认就是 8080
server.port=8080
# 配置项目路径:localhost:8080/community
server.servlet.context-path=/community
# 关闭模板缓存:开发时关闭,为了修改不延迟;上线时开启,减低服务器压力。
spring.thymeleaf.cache=false
@RequestMapping:映射请求,path 映射路径,method 指定请求方式。
@ResponseBody:作用方法上,表示将返回结果直接写入 HTTP response body,不注解会返回网页。
@RequestParam:设置请求的相关参数,包括 name、required、defaultValue,比如:
http://localhost:8080/community/alpha/students:打印 1,10
http://localhost:8080/community/alpha/students?current=2&limit=20:打印 2,20
@Controller
@RequestMapping("/alpha")
public class AlphaController {
// 访问路径:http://localhost:8080/community/alpha/students
@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";
}
}
@PathVariable:设置路径变量,http://localhost:8080/community/alpha/student/666,得到 666
@RequestMapping(path = "/student/{id}",method = RequestMethod.GET)
@ResponseBody
public String getStudent(@PathVariable("id") int id){
System.out.println(id);
return "a student";
}
提交表单
GET 请求传参在明面上,并且地址路径的长度有限制(即 GET 传参有限),所以提交数据一般用 POST;
方法参数名跟表单 input 里 name 一致,就可以直接传过来。
<form method="post" action="/community/alpha/student">
<p>姓名:<input type="text" name="name"></p>
<p>年龄:<input type="text" name="age"></p>
<p><input type="submit" value="保存"></p>
</form>
// POST 请求
@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 数据
// ModelAndView:数据、视图的封装类;addObject:添加数据,setViewName:设置视图;
@RequestMapping(path = "/teacher",method = RequestMethod.GET)
public ModelAndView getTeacher(){
ModelAndView mav = new ModelAndView();
mav.addObject("name","WhyNotYue");
mav.addObject("age",18);
mav.setViewName("/demo/view");
return mav;
}
另一种简化写法(开发推荐写法):
// 不注 @ResponseBody,返回就是 HTML 数据(网页);
// Model 由 DispatcherServlet 来传入,负责封装数据;addAttribute:添加数据。
@RequestMapping(path = "/school",method = RequestMethod.GET)
public String getSchool(Model model){
model.addAttribute("name","蓝翔技术大学");
model.addAttribute("age",10);
return "/demo/view";
}
<!--告诉服务器这是 thymeleaf 模板引擎-->
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<body>
<!--从 ModelAndView / Model 中提取 name 对应数据-->
<p th:text = "${name}"></p>
<p th:text = "${age}"></p>
</body>
</html>
响应 JSON 数据(异步请求)
异步请求:比如,注册时输入昵称,不刷新网页,它就能判断昵称是否重复。
JSON 起一个中介作用:Java 对象 -> JSON 字符串 -> JS 对象。
一组数据:用 Map 封装,结果(一个 JSON 字符串):{“name”:“yue”,“salary”:10000.0,“age”:18}
@RequestMapping(path = "/emp",method = RequestMethod.GET)
@ResponseBody
public Map<String,Object> getEmp(){
Map<String,Object> map = new HashMap<>();
map.put("name","yue");
map.put("age",18);
map.put("salary",10000.00);
return map;
}
多组数据:用 List<Map>
封装,结果(集合形式 JSON 字符串):[{“name”:“yue”,“salary”:10000.0,“age”:18},{“name”:“yue2”,“salary”:12000.0,“age”:19},{“name”:“yue3”,“salary”:14000.0,“age”:20}]
@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","yue");
emp.put("age",18);
emp.put("salary",10000.00);
list.add(emp);
emp = new HashMap<>();
emp.put("name","yue2");
emp.put("age",19);
emp.put("salary",12000.00);
list.add(emp);
emp = new HashMap<>();
emp.put("name","yue3");
emp.put("age",20);
emp.put("salary",14000.00);
list.add(emp);
return list;
}