2.4.@PathVariable
Controller编写:
@Slf4j @Controller @RequestMapping("/csdn") public class IndexController { @RequestMapping("/list03/{id}") public String list03(@PathVariable("id") Integer id) { log.info("@PathVariable: id{}",id); return "hello"; } }
此时请求映射的请求路径为:
http://localhost:8080/Spring_MyBatis/csdn/list03/7
2.5.@RequestBody
讲解@RequestBody之前,将大家推荐postman或者apipost/eolink等工具发送请求数据。
这些工具可以帮助您模拟发送请求和接收响应,以便更好地理解@RequestBody参数的含义和使用方法。
Controller编写:
@Slf4j @Controller @RequestMapping("/csdn") public class IndexController { @RequestMapping("/list04") public String list04(Map map) { log.info("没有注解: Map:{}",map); return "hello"; } @RequestMapping("/list05") public String list05(@RequestBody Map map) { log.info("@ResponseBody: Map:{}",map); return "hello"; } }
此时请求映射的请求路径为:
http://localhost:8080/Spring_MyBatis/csdn/list04
测试没有注解的
结果是没有获取到参数
测试有注解的
可以获取到参数结果
2.6.@RequestHeader
Controller编写:
@Slf4j @Controller @RequestMapping("/csdn") public class IndexController { @RequestMapping("/list06") public String list06(@RequestHeader String jwt) { log.info("@RequestHeader: jwt:{}",jwt); return "hello"; } }
此时请求映射的请求路径为:
http://localhost:8080/Spring_MyBatis/csdn/list06
三、返回值
为了方便模拟效果,借助ResponseUtil工具类,ResponseUtil类提供了一种方便的方式来将对象以文本或JSON格式写入HTTP响应流中,以便在Web应用程序中向客户端返回数据。
package com.liwen.util; import com.fasterxml.jackson.databind.ObjectMapper; import javax.servlet.http.HttpServletResponse; import java.io.PrintWriter; public class ResponseUtil { public static void write(HttpServletResponse response,Object o)throws Exception{ response.setContentType("text/html;charset=utf-8"); PrintWriter out=response.getWriter(); out.println(o.toString()); out.flush(); out.close(); } public static void writeJson(HttpServletResponse response,Object o)throws Exception{ ObjectMapper om = new ObjectMapper(); // om.writeValueAsString(o)代表了json串 write(response, om.writeValueAsString(o)); } }
pom.xml依赖
<dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> <version>2.12.4</version> <!-- 请根据您的需求选择合适的版本 --> </dependency>
3.1.void
处理器对请求处理后,无需跳转到其它任何资源,此时可以让处理器方法返回 void。
Controller编写:
@Slf4j @Controller @RequestMapping("/csdn") public class IndexController { @RequestMapping("/return01") public void return01(HttpServletResponse resp){ Map<String,Object> map=new HashMap<>(); map.put("code",200); map.put("msg","成功添加。。。"); try { ResponseUtil.writeJson(resp,map); } catch (Exception e) { e.printStackTrace(); } } }
3.2.String
通过http://localhost:8080/Spring_MyBatis/csdn/return02访问请求方法,并经过视图解析器跳转指定页面。
Controller编写:
@Slf4j @Controller @RequestMapping("/csdn") public class IndexController { @RequestMapping("/return02") public String return02( ){ return "hello"; } }
网页效果:
3.3.String+Model
通过http://localhost:8080/Spring_MyBatis/csdn/return03访问请求方法,并经过视图解析器跳转指定页面。
Controller编写:
@Slf4j @Controller @RequestMapping("/csdn") public class IndexController { @RequestMapping("/return03") public String return03(Model Model,HttpServletRequest req){ Model.addAttribute("name","Java文昊"); req.setAttribute("age",18); return "hello"; } }
JSP页面:
<%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>Title</title> </head> <body> 用户名:${name} 年龄:${age} </body> </html>
3.4.ModelAndView
Controller编写:
@Slf4j @Controller @RequestMapping("/csdn") public class IndexController { @RequestMapping("/return04") public ModelAndView return04(){ ModelAndView mv=new ModelAndView(); mv.addObject("name","湖南彭于晏-文昊桑"); mv.addObject("age",18); mv.setViewName("hello"); return mv; } }
JSP页面:
<%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>Title</title> </head> <body> <br>用户名:${name} 年龄:${age} </body> </html>
四、页面跳转
转发(forward:path)和重定向(redirect:path)这两种跳转方式将会绕开视图解析器的前缀和后缀;还有就是如果是在同一controller中则不用使用"/"从根目录开始,而如果是在不同的controller则一定要从根目录开始。
4.1.转发forward
- 当前类
Controller编写:
// 转发到当前类 @RequestMapping("/forward01") public String forward01(){ return "forward:return02"; } @RequestMapping("/return02") public String return02( ){ return "hello"; }
JSP页面:
<%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>Title</title> </head> <body> 用户名:${name} 年龄:${age} </body> </html>
- 其他类
Controller编写:
// 转发到其他类 @RequestMapping("/forward02") public String forward02(){ return "forward:/hello/requestTest"; } @Controller @RequestMapping("/hello") public class hello { @RequestMapping("/requestTest") public String Hello(){ return "text"; } }
JSP页面
<%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>Title</title> </head> <body> 你好我是其他类跳转过来的 </body> </html>
4.2.重定向redirect
- 当前类
Controller编写:
// 重定向到当前类 @RequestMapping("/redirect01") public String redirect01(){ return "redirect:return02"; } @RequestMapping("/return02") public String return02( ){ return "hello"; }
JSP页面:
<%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>Title</title> </head> <body> 用户名:${name} 年龄:${age} </body> </html>
- 其他类
Controller编写:
// 重定向到其他类 @RequestMapping("/redirect02") public String redirect02(){ return "redirect:/hello/requestTest"; } @Controller @RequestMapping("/hello") public class hello { @RequestMapping("/requestTest") public String Hello(){ return "text"; } }
JSP页面:
<%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>Title</title> </head> <body> 你好我是其他类跳转过来的 </body> </html>