'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?
Solution 1:[1]
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.
Solution 2:[2]
Actually, a @RestController
can also return view:
Set the controller method's return type to be
ModelAndView
Set your view path like this:
ModelAndView mav = new ModelAndView("userCenter/loginPage");
return mav;
You get the correct JSO page view content.
Solution 3:[3]
Check the API docs for @RestController it is annotated with @ResponseBody itself, which indicates that method return value should be bound to the web response body. The configured view objects will never come into play, so it cannot map to any view. My question is why you want to use the RestController anyway, its not meant to map to any views?
Solution 4:[4]
Could still be useful with @RestController for creating redirect fallback to you REST Api
@GetMapping("/**")
public ModelAndView Default()
{
ModelAndView mav = new ModelAndView("redirect:/actuator/health");
return mav;
}
Solution 5:[5]
@RestController
configures @ResponseBody
for every body automatically, it means it is designed to write everything on the output and not to return views. If you want to return a view, configure another class with @Controller
and return that view accordingly.
Solution 6:[6]
@RestController actually returns the HTTP response as JSON or XML only.
- @RestController is internally annotated with @ResponseBody.
- @ResponseBody annotation tells a controller that the object returned is automatically serialized into JSON and passed back into the HttpResponse object.
But if you want to return as JSP, you can use ModelAndView like below.
example:
@RestController
public class HomeController {
@RequestMapping("/")
public ModelAndView getHome() {
ModelAndView mav = new ModelAndView("home.jsp");
return mav;
}
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | |
Solution 2 | aboger |
Solution 3 | varun |
Solution 4 | Laurent de Laprade |
Solution 5 | aboger |
Solution 6 | veryreverie |