springMVC注解@Controller和@RestController的区别,事关初学者对java引擎模板的使用

如果只是使用@RestController注解Controller,则Controller中的方法无法返回jsp页面,或者html,配置的视图解析器 InternalResourceViewResolver不起作用,返回的内容就是Return 里的内容。

如果需要返回到指定页面,则需要用 @Controller配合视图解析器InternalResourceViewResolver才行。

值得注意的是,如果项目中使用了@RestController来修饰Controller那么方法的返回结果需要是ModelAndView 才可以使用model中的值

@RequestMapping("/helloHtml")
public ModelAndView helloHtml(Model model) {
model.addAttribute("nameKey", "Thymeleaf");
return new ModelAndView("hello");
}

https://www.cnblogs.com/shuaifing/p/8119664.html

posted @ 2020-08-16 22:44  g青苹果  阅读(15)  评论(0编辑  收藏  举报