知识点:
1)、返回值类型为String,请求转发: return ``"forward:/success"``;。
2)、web层的类,返回值为String类型的方法。类只能使用@Controller注解,不能够使用@RestController注解,因为@RestController是包含了@ResponseBody的。而@ResponseBody是直接把return的数据输出在浏览器上,所以达不到请求转发的目的。
3)、@RequestAttribute``(``"msg"``) ``String ``msg可以获取request.setAttribute("msg","成功了。。。"); request域中的数据。
4)、方法形参中 HttpServletRequest ``request 也可以通过Object attribute ``= request.getAttribute(``"msg"``);获取request域中的数据。
package com.wzy.springbootweb01.controller;@Controllerpublic class MyController {/*"application/json;charset=utf-8"*/@GetMapping(value = "/attribute")public String attribute(HttpServletRequest request, HttpServletResponse response) throws Exception{request.setAttribute("msg","成功了。。。");request.setAttribute("code",200);//request.getRequestDispatcher("success").forward(request,response);return "forward:/success";}@ResponseBody@GetMapping(value = "/success")public Map successs(@RequestAttribute("msg") String msg,@RequestAttribute("code") Integer code,HttpServletRequest request){Map<String,Object> map = new HashMap<>();Object attribute = request.getAttribute("msg");map.put("attribute",attribute);map.put("msg",msg);map.put("code",code);return map;}}
