Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
982 views
in Technique[技术] by (71.8m points)

spring - how to use spring4 @RestController to return a jsp page?

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";
    }
}

It is ok, I get the the loginPage.jsp right content in browser.

but when I change @Controller to @RestController

the localhost:8080//userCenter/loginPage return a page with the string "userCenter/loginPage"

So,how could I use @RestController to get jsp pages like @Controller?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.

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.

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...