这是我今天实现跳转的后台代码
@RestController
public class requestController {
@GetMapping("/requesttest")
public String requesttest(HttpServletRequest request){
request.setAttribute("msg","nmsl");
request.setAttribute("code","112233");
return "forward:/success";
}
@GetMapping("/success")
public Map<String, Object> success(@RequestAttribute("msg") String msg,
@RequestAttribute("code") String code,
HttpServletRequest request){
Map<String,Object> map=new HashMap();
map.put("msg",msg);
map.put("code",code);
map.put("request_msg",request.getAttribute("msg"));
map.put("request_code",request.getAttribute("code"));
return map;
}
}
前台代码如下
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>index</title>
</head>
<body>
<a href="/requesttest">requesttest</a>
</body>
</html>
我们执行
这里跳转失败的原因是我们给控制层的注解是
@RestController
该注解会使得该controller之下所有方法默认添加一个
@ResponseBody
这会使得返回的数据变成json数据类型,所以无法执行跳转
我们把@RestController改成@Controller
执行
成功