A view resolver in Spring MVC turns the logical view name returned by a controller (for example "home") into a concrete View that renders the response. You configure one by registering a ViewResolver bean, by using ViewResolverRegistry in a WebMvcConfigurer, or, in Spring Boot, by setting properties such as spring.mvc.view.prefix and spring.thymeleaf.prefix.
Several resolvers can coexist. Spring asks them in order and the first one that returns a non-null View wins, so ordering matters as soon as you mix template technologies.
A controller method returns a view name, a ModelAndView or a String. The DispatcherServlet passes that name to each ViewResolver bean, sorted by Ordered.getOrder(). The first resolver that returns a View renders the model into the response.
The name home only becomes a file such as /WEB-INF/views/home.jsp or templates/home.html because a resolver adds a prefix and a suffix. Methods annotated with @ResponseBody or @RestController skip view resolution entirely.
| Resolver | Used for | Notes |
|---|---|---|
InternalResourceViewResolver | JSP and servlet forwards | Resolves any name, so it must be last in the chain |
ThymeleafViewResolver | Thymeleaf templates | Provided by thymeleaf-spring6, auto-configured by Spring Boot |
FreeMarkerViewResolver | FreeMarker templates | Auto-configured by Spring Boot with spring.freemarker.* |
ContentNegotiatingViewResolver | Picks among other resolvers by media type | Delegates to the other resolvers it finds |
BeanNameViewResolver | Views defined as beans | Useful for custom View classes such as PDF or Excel exports |
In a plain Spring Framework application, implement WebMvcConfigurer and use ViewResolverRegistry. This is the preferred style over declaring XML beans.
registry.jsp(prefix, suffix) registers an InternalResourceViewResolver configured for JSP. If you need to tune the resolver yourself, declare it as a bean:
With Spring Framework 6 and Spring Boot 3 the servlet API is jakarta.*, so JSP pages need a Jakarta-compatible JSTL.
Spring Boot auto-configures resolvers when the matching library is on the classpath. For most projects you only set properties.
Add spring-boot-starter-thymeleaf for Thymeleaf or spring-boot-starter-freemarker for FreeMarker, then place templates under src/main/resources/templates. JSP is the awkward one: it works in a WAR deployed to a standalone servlet container, but embedded executable JARs have known limitations with JSP, so a modern template engine is the safer choice for new work.
Every resolver that implements Ordered (all the standard ones do) can be given an order. A lower number runs first.
setViewNames restricts a resolver to matching names, which lets two template engines live side by side. Keep InternalResourceViewResolver at the lowest precedence because it never returns null; any resolver after it would be unreachable.
Spring recognises two special prefixes in a view name and handles them before the template lookup:
redirect:/orders sends an HTTP redirect to /orders.forward:/legacy/path forwards the request internally without the browser noticing.No. Methods that return JSON through @RestController or @ResponseBody are written by an HttpMessageConverter, not by a view resolver.
Usually the prefix or suffix is wrong, the template is not in the expected folder, or a JSP view name equals the request path, which makes the forward loop back to the same servlet. Check the resolved path in the debug log for org.springframework.web.servlet.
Yes. Give each resolver an order and restrict them with setViewNames, with Thymeleaf first and InternalResourceViewResolver last.
Thymeleaf is the default choice in the Spring ecosystem and has first-class Boot support. FreeMarker and Mustache are alternatives, and JSP is mostly kept for existing applications.
A view resolver maps logical view names to renderable views. Use WebMvcConfigurer.configureViewResolvers or Spring Boot properties for the common cases, order multiple resolvers explicitly, keep InternalResourceViewResolver last, and prefer Thymeleaf or FreeMarker over JSP for new Spring Boot applications.