Spring MVC Views
In Spring MVC, views are used to render the model data and present it to the user. This guide covers the key concepts of Spring MVC views, including configuring view resolvers, using JSPs, Thymeleaf, and other view technologies.
Key Concepts of Spring MVC Views
- 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.
- Model: Holds data to be rendered in the view.
Configuring View Resolvers
View resolvers are used to map logical view names to actual view implementations. Here is an example of configuring an InternalResourceViewResolver
for JSP views:
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>
Using JSP Views
JSP (JavaServer Pages) is a common view technology used in Spring MVC. Here is an example of a simple JSP view:
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>
Using Thymeleaf Views
Thymeleaf is a modern server-side Java template engine for web and standalone environments. Here is an example of configuring a ThymeleafViewResolver
:
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>
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";
}
}
Handling Model Data
Model data is used to pass information from the controller to the view. Here is an example of adding attributes to the model:
Example:
// 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("/home")
public String home(Model model) {
model.addAttribute("message", "Welcome to Spring MVC!");
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.
- Model: Holds data to be rendered in the view.
- 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 views are used to render model data and present it to the user. 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 views and handling model data effectively enhances the user experience. Happy coding!