Swiftorial Logo
Home
Swift Lessons
Tutorials
Learn More
Career
Resources

Spring View Resolvers

In Spring MVC, view resolvers are used to resolve the logical view names returned by controllers into actual view implementations. This guide covers the key concepts and usage of view resolvers in Spring MVC, including configuring view resolvers for JSP, Thymeleaf, and other view technologies.

Key Concepts of Spring View Resolvers

  • View Resolver: Resolves logical view names to actual view implementations.
  • InternalResourceViewResolver: A view resolver for JSP-based views.
  • ThymeleafViewResolver: A view resolver for Thymeleaf-based views.
  • BeanNameViewResolver: Resolves view names to bean names.
  • UrlBasedViewResolver: Base class for view resolvers that support direct resolution of logical view names to URLs.

Configuring InternalResourceViewResolver

The InternalResourceViewResolver is used for resolving JSP views. Here is an example configuration:

dispatcher-config.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd
       http://www.springframework.org/schema/context
       http://www.springframework.org/schema/context/spring-context.xsd
       http://www.springframework.org/schema/mvc
       http://www.springframework.org/schema/mvc/spring-mvc.xsd">

    <mvc:annotation-driven />

    <context:component-scan base-package="com.example.springmvc.controllers" />

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

home.jsp

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<html>
<head>
    <title>Home</title>
</head>
<body>
    <h1>${message}</h1>
</body>
</html>

Configuring ThymeleafViewResolver

The ThymeleafViewResolver is used for resolving Thymeleaf views. Here is an example configuration:

dispatcher-config.xml (with Thymeleaf)

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xmlns:th="http://www.thymeleaf.org/schema/thymeleaf"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd
       http://www.springframework.org/schema/context
       http://www.springframework.org/schema/context/spring-context.xsd
       http://www.springframework.org/schema/mvc
       http://www.springframework.org/schema/mvc/spring-mvc.xsd
       http://www.thymeleaf.org/schema/thymeleaf
       http://www.thymeleaf.org/schema/thymeleaf/thymeleaf-spring4.xsd">

    <mvc:annotation-driven />

    <context:component-scan base-package="com.example.springmvc.controllers" />

    <bean id="templateResolver" class="org.thymeleaf.spring4.templateresolver.SpringResourceTemplateResolver">
        <property name="prefix" value="/WEB-INF/views/" />
        <property name="suffix" value=".html" />
        <property name="templateMode" value="HTML5" />
    </bean>

    <bean id="templateEngine" class="org.thymeleaf.spring4.SpringTemplateEngine">
        <property name="templateResolver" ref="templateResolver" />
    </bean>

    <bean class="org.thymeleaf.spring4.view.ThymeleafViewResolver">
        <property name="templateEngine" ref="templateEngine" />
    </bean>
</beans>

home.html (Thymeleaf)

<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <title>Home</title>
</head>
<body>
    <h1 th:text="${message}">Home</h1>
</body>
</html>

Configuring BeanNameViewResolver

The BeanNameViewResolver resolves view names to bean names. Here is an example configuration:

dispatcher-config.xml (with BeanNameViewResolver)

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd
       http://www.springframework.org/schema/context
       http://www.springframework.org/schema/context/spring-context.xsd
       http://www.springframework.org/schema/mvc
       http://www.springframework.org/schema/mvc/spring-mvc.xsd">

    <mvc:annotation-driven />

    <context:component-scan base-package="com.example.springmvc.controllers" />

    <bean class="org.springframework.web.servlet.view.BeanNameViewResolver"/>

    <bean name="homeView" class="org.springframework.web.servlet.view.JstlView">
        <property name="url" value="/WEB-INF/views/home.jsp"/>
    </bean>
</beans>

Using UrlBasedViewResolver

The UrlBasedViewResolver is a base class for view resolvers that support direct resolution of logical view names to URLs. Here is an example configuration:

dispatcher-config.xml (with UrlBasedViewResolver)

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd
       http://www.springframework.org/schema/context
       http://www.springframework.org/schema/context/spring-context.xsd
       http://www.springframework.org/schema/mvc
       http://www.springframework.org/schema/mvc/spring-mvc.xsd">

    <mvc:annotation-driven />

    <context:component-scan base-package="com.example.springmvc.controllers" />

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

Controller Example

Here's a simple controller that returns views for JSP and Thymeleaf:

HomeController.java

// HomeController.java
package com.example.springmvc.controllers;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;

@Controller
public class HomeController {
    @GetMapping("/jsp")
    public String homeJsp(Model model) {
        model.addAttribute("message", "Welcome to Spring MVC with JSP!");
        return "home";
    }

    @GetMapping("/thymeleaf")
    public String homeThymeleaf(Model model) {
        model.addAttribute("message", "Welcome to Spring MVC with Thymeleaf!");
        return "home";
    }
}

Key Points

  • View Resolver: Resolves logical view names to actual view implementations.
  • InternalResourceViewResolver: A view resolver for JSP-based views.
  • ThymeleafViewResolver: A view resolver for Thymeleaf-based views.
  • BeanNameViewResolver: Resolves view names to bean names.
  • UrlBasedViewResolver: Base class for view resolvers that support direct resolution of logical view names to URLs.
  • Configure view resolvers to map logical view names to actual views.
  • Use JSP or Thymeleaf for rendering views in Spring MVC.
  • Add attributes to the model to pass data from the controller to the view.

Conclusion

Spring MVC view resolvers are used to resolve logical view names returned by controllers into actual view implementations. By configuring view resolvers and using technologies like JSP and Thymeleaf, developers can create dynamic and interactive web pages. Understanding the key concepts of Spring MVC view resolvers enhances the user experience. Happy coding!