Most Commonly Used ViewResolver in Spring MVC

Answered using GPT-4o-mini by Java Q&A Hub

Question:

Which ViewResolver class is widely used?

Answer:

In Spring MVC, the most widely used ViewResolver class is the InternalResourceViewResolver. This class is used to resolve views by prefixing and suffixing the view names with a specified URL pattern, typically for JSP files.

For example, if you configure an InternalResourceViewResolver with a prefix of /WEB-INF/views/ and a suffix of .jsp, a view name returned by a controller like "home" would be resolved to /WEB-INF/views/home.jsp.

Here's a simple configuration example in a Spring application context:

<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="prefix" value="/WEB-INF/views/" />
    <property name="suffix" value=".jsp" />
</bean>

In addition to InternalResourceViewResolver, there are other view resolvers like BeanNameViewResolver, XmlViewResolver, and ThymeleafViewResolver (for Thymeleaf templates), but InternalResourceViewResolver is the most commonly used for JSP-based applications.