起因:
开发的时候遇到一个问题,明明return的时候应该调到comment下的postComment页面的,但是结果却是页面显示“comment/postComment”几个大字,最后才发现用错了controller的注解,用了@RestController注解
问题分析:
@Controller和@RestController的区别:@RestController注解相当于@ResponseBody + @Controller合在一起的作用
-
如果只使用@RestController去注解Controller,那么Controller中的方法是无法返回jsp页面或者html的,配置的视图解析器 InternalResourceViewResolver不起作用,返回的就是Return 的内容,就是上图的comment/postComment。
-
如果需要返回到指定页面,则需要用 @Controller配合视图解析器InternalResourceViewResolver才行。如果需要返回JSON,XML或自定义mediaType内容到页面,则需要在对应的方法上加上@ResponseBody注解。