Spring MVC
- 三层架构
- 表现层
- 业务层
- 数据访问层
- MVC(处理表现层)
- Model:模型层
- View:视图层
- Controller:控制层
底层请求方式
在controller中添加
@RequestMapping("/http")public void http(HttpServletRequest request, HttpServletResponse response) throws IOException {//获取请求数据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");PrintWriter printWriter = response.getWriter();printWriter.write("<h1>Header<h1>");printWriter.close();}
运行并打开http://localhost:8081/community/alpha/http
GET请求
方式一:以键值对方式引入参数
// /students?current=1&limit=20@RequestMapping(path = "/students", method = RequestMethod.GET)@ResponseBodypublic 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 "students";}
方式二:在路径中引入参数
// /student/2019213151@RequestMapping(path = "/student/{id}", method = RequestMethod.GET)@ResponseBodypublic String getStudent(@PathVariable("id") int id) {System.out.println(id);return "a student " + id;}
访问http://localhost:8081/community/alpha/student/101