如何使用spring4 @RestController返回一个jsp页面? [英] how to use spring4 @RestController to return a jsp page?
问题描述
当我像这样使用spring3 @Controller时:
When I use spring3 @Controller like this:
@RequestMapping("/userCenter")
@Controller
public class LoginCtrl {
@RequestMapping("/loginPage")
public String login(HttpServletRequest request,HttpServletResponse response,Model model) throws Exception {
return "userCenter/loginPage";
}
}
没关系,我在浏览器中获得了loginPage.jsp正确的内容.
It is ok, I get the the loginPage.jsp right content in browser.
但是当我将@Controller更改为@RestController
but when I change @Controller to @RestController
localhost:8080//userCenter/loginPage返回带有字符串"userCenter/loginPage"的页面
the localhost:8080//userCenter/loginPage return a page with the string "userCenter/loginPage"
那么,如何使用@RestController来获取像@Controller这样的jsp页面?
So,how could I use @RestController to get jsp pages like @Controller?
推荐答案
不应该. @RestController
并不意味着要通过String
返回类型/值返回视图名称.这意味着返回将直接写入响应主体的内容.
You shouldn't. A @RestController
is not meant to return view names through a String
return type/value. It's meant to return something that will be written to the response body directly.
更具体地说(在一般配置情况下),Spring MVC在RequestMappingHandlerAdapter#getDefaultReturnValueHandlers()
中配置其返回值处理程序.如果您查看该实现,则String
视图名称ViewNameMethodReturnValueHandler
的处理程序将在之后注册到@RestController
(实际上是@ResponseBody
)RequestResponseBodyMethodProcessor
的处理程序中.
More concretely (in the general configuration case), Spring MVC configures its return value handlers in RequestMappingHandlerAdapter#getDefaultReturnValueHandlers()
. If you look at that implementation, the handler for String
view names, ViewNameMethodReturnValueHandler
, is registered after the handler for @RestController
(really @ResponseBody
), RequestResponseBodyMethodProcessor
.
如果确实需要,可以声明您的方法具有View
或ModelAndView
的返回类型(这些处理程序ViewMethodReturnValueHandler
和ModelAndViewMethodReturnValueHandler
的注册在RequestResponseBodyMethodProcessor
之前)并返回适当的对象,并带有可识别的视图名称.
If you really have to, you can declare your method to have a return type of View
or ModelAndView
(the handlers for these, ViewMethodReturnValueHandler
and ModelAndViewMethodReturnValueHandler
, are registered before RequestResponseBodyMethodProcessor
) and return the appropriate object, with an identifying view name.
这篇关于如何使用spring4 @RestController返回一个jsp页面?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!