Spring MVC uses view parsing
In Spring MVC, whether to use view resolution depends on the return type and behavior of the controller method.
The following are detailed instructions:
1. View parsing situation
Spring MVC uses a view resolver to resolve logical view names to actual views in the following cases:
(1) Controller method returns string
When the controller method returns a string, Spring MVC treats it as a logical view name and resolves it to the actual view through the view parser.
@GetMapping("/home") public String home() { return "home"; // Logical view name}
- The view parser will
home
Resolved as/WEB-INF/views/
(The path depends on the configuration).
(2) Controller method returnsModelAndView
When the controller method returnsModelAndView
Spring MVC resolves using the view name in it.
@GetMapping("/profile") public ModelAndView profile() { ModelAndView modelAndView = new ModelAndView("profile"); // Logical view name ("user", ()); return modelAndView; }
(3) Controller method returnsvoid
ornull
When the controller method returnsvoid
ornull
Spring MVC automatically infers the view name based on the request path.
@GetMapping("/about") public void about() { // The default view name is "/about"}
(4) Controller method returns view object
When the controller method returns directlyView
When an object is used, Spring MVC skips the view parser and uses the view directly.
@GetMapping("/customView") public View customView() { return new MyCustomView(); // Custom view object}
2. View parsing is not used
Spring MVC does not use the view resolver in the following cases:
(1) Use@ResponseBody
annotation
When the controller method is used@ResponseBody
When annotating, the return value will be written directly to the HTTP response body and will not be parsed.
@GetMapping("/data") @ResponseBody public String data() { return "This is data"; // Write directly to the response body}
(2) Use@RestController
annotation
When the controller class is used@RestController
When annotating, the return values of all methods are written directly to the HTTP response body and will not be parsed.
@RestController public class ApiController { @GetMapping("/api/data") public String apiData() { return "API Data"; } }
(3) ReturnResponseEntity
Object
When the controller method returnsResponseEntity
When Spring MVC writes the response body and status code directly to the HTTP response, and will not perform view parsing.
@GetMapping("/response") public ResponseEntity<String> response() { return ("Response Data"); }
(4) Returnvoid
and write the response directly
When the controller method returnsvoid
, and directly passHttpServletResponse
When a response is written, no view parsing is performed.
@GetMapping("/directResponse") public void directResponse(HttpServletResponse response) throws IOException { ().write("Direct Response"); }
(5) UseRedirectView
or redirect prefix
When the controller method returnsRedirectView
Or useredirect:
When prefixed, Spring MVC will directly redirect to the specified URL and will not perform view resolution.
@GetMapping("/redirect") public String redirect() { return "redirect:/newUrl"; // Direct redirection}
(6) Useforward:
Prefix
When the controller method is usedforward:
When prefixed, Spring MVC will forward directly to the specified URL and will not perform view resolution.
@GetMapping("/forward") public String forward() { return "forward:/newUrl"; // Forward directly}
Summarize
Whether Spring MVC uses view resolution depends on the return type and behavior of the controller method:
View parsing is used:
- Returns the string (logical view name).
- return
ModelAndView
。 - return
void
ornull
(Automatically infer view name). - Returns the view object (skip the view parser, but belongs to the view parsing process).
View parsing is not used:
- use
@ResponseBody
or@RestController
。 - return
ResponseEntity
。 - Write directly
HttpServletResponse
。 - use
redirect:
orforward:
Prefix. - return
RedirectView
。
By understanding these rules, you can better control the view parsing behavior of Spring MVC and avoid unnecessary parsing or errors.
The above is personal experience. I hope you can give you a reference and I hope you can support me more.