1.重定向
用户可以观察到路径变化
请求多于一次
在conttroller中的实现转发(我认为最简单的)
@Controller
public class Controller {
@GetMapping("/aaa")
public String toward(){
return "redirect:index.html";
}
}
结果 url变化为localhost:8080/index.html,页面显示的为index.html页面
2.转发
用户无路径上的变化
只发起一次请求
转发在路径上并没有任何变化,只是将页面换成了新的页面
在conttroller中的实现转发(我认为最简单的)
@Controller
public class Controller {
@GetMapping("/aaa")
public String toward(){
return "forward://index.html";
}
}
结果 url没有任何变化(仍然为localhost:8080/aaa),页面显示的为index.html页面
3.特别注意
在使用转发和重定向时controller层的注解只能使用@Controller,而不能使用@RestController
@RestController相当于@ResponseBody+@Controller
@ResponseBody将会使执行过程去除视图解析器部分,所以重定向和转发不会被
识别(即将把返回的值转换为json串格式,所以会使得重定向或转发失效)