SoFunction
Updated on 2025-04-13

Interpretation of Spring MVC's problem of using view analysis

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 willhomeResolved as/WEB-INF/views/(The path depends on the configuration).

(2) Controller method returnsModelAndView

When the controller method returnsModelAndViewSpring 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 returnsvoidornull

When the controller method returnsvoidornullSpring 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 directlyViewWhen 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@ResponseBodyannotation

When the controller method is used@ResponseBodyWhen 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@RestControllerannotation

When the controller class is used@RestControllerWhen 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) ReturnResponseEntityObject

When the controller method returnsResponseEntityWhen 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) Returnvoidand write the response directly

When the controller method returnsvoid, and directly passHttpServletResponseWhen a response is written, no view parsing is performed.

@GetMapping("/directResponse")
public void directResponse(HttpServletResponse response) throws IOException {
    ().write("Direct Response");
}

(5) UseRedirectViewor redirect prefix

When the controller method returnsRedirectViewOr 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).
  • returnModelAndView
  • returnvoidornull(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@ResponseBodyor@RestController
  • returnResponseEntity
  • Write directlyHttpServletResponse
  • useredirect:orforward:Prefix.
  • returnRedirectView
    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.