Spring MVC Tutorial
Core Spring MVC
Spring MVC - Annotation
Spring MVC - Form Handling
Spring MVC with JSTL
Spring MVC with REST API
Spring MVC with Database
Creating a view in Spring MVC is the step where you design how the data will be presented to the user. The view forms the 'V' in the MVC (Model-View-Controller) pattern used by Spring MVC.
Spring MVC supports a variety of view technologies, including JSP, Thymeleaf, FreeMarker, and others. Here, we'll use Thymeleaf as it's becoming increasingly popular with Spring Boot applications.
First, make sure to add the necessary dependencies. For a Spring Boot project using Thymeleaf, you'd add:
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency>
Inside the src/main/resources/templates
directory of your Spring Boot project, create a new HTML file called greeting.html
.
Here's a simple template to display a greeting:
<!DOCTYPE html> <html xmlns:th="http://www.thymeleaf.org"> <head> <title>Greeting</title> </head> <body> <h2 th:text="${message}"></h2> </body> </html>
In the above, the th:text="${message}"
is a Thymeleaf-specific attribute that sets the text content of the <h2>
element to the value of the message
attribute provided from the model.
Now, you'd create a controller to set up the model and specify which view to use:
@Controller public class GreetingController { @GetMapping("/greet") public String greet(Model model) { model.addAttribute("message", "Hello from Spring MVC!"); return "greeting"; // This refers to the 'greeting.html' Thymeleaf template } }
Run your Spring Boot application. If you navigate to http://localhost:8080/greet
in your web browser, you should see the message "Hello from Spring MVC!" displayed on the page.
If you're using JSP or another view technology, the setup is slightly different:
src/main/webapp/WEB-INF/views/
. You'd need the appropriate dependencies and might need additional configuration in application.properties
.Creating a view in Spring MVC is about designing your webpage templates and then using controllers to populate those templates with data. With the integration of view technologies like Thymeleaf, this process becomes streamlined and works seamlessly with Spring Boot applications.
Creating a view in Spring MVC example:
Description: In Spring MVC, a view is responsible for rendering the data provided by the controller. Views can be JSP, Thymeleaf templates, or other template engines.
Code snippet (Java):
@Controller @RequestMapping("/example") public class ExampleController { @GetMapping("/showView") public String showView() { // Returning the view name return "showView"; } }
Code snippet (JSP - JavaServer Pages):
<!-- JSP view to display a message --> <html> <body> <h2>Hello, this is a Spring MVC view!</h2> </body> </html>
Your first Spring MVC view step-by-step:
Description: A step-by-step guide to creating your first Spring MVC view involves defining a controller method and returning the name of the view.
Code snippet (Java):
@Controller @RequestMapping("/first") public class FirstController { @GetMapping("/welcome") public String welcomeView() { // Returning the view name return "welcomeView"; } }
Code snippet (JSP - JavaServer Pages):
<!-- JSP view to welcome the user --> <html> <body> <h2>Welcome to Spring MVC!</h2> </body> </html>
Introduction to Spring MVC view development:
Description: An introduction to Spring MVC view development involves understanding the role of views in the MVC architecture and how they are used to present data to users.
Code snippet (Java):
@Controller @RequestMapping("/intro") public class IntroController { @GetMapping("/displayView") public String displayView() { // Returning the view name return "displayView"; } }
Code snippet (JSP - JavaServer Pages):
<!-- JSP view to display a message --> <html> <body> <h2>This is a Spring MVC view!</h2> </body> </html>
How to set up views in Spring MVC:
Description: Setting up views in Spring MVC involves configuring view resolvers, which map logical view names to actual view implementations.
Code snippet (Java):
@Configuration @EnableWebMvc @ComponentScan(basePackages = "com.example") public class WebConfig implements WebMvcConfigurer { @Bean public InternalResourceViewResolver viewResolver() { InternalResourceViewResolver resolver = new InternalResourceViewResolver(); resolver.setPrefix("/WEB-INF/views/"); resolver.setSuffix(".jsp"); return resolver; } }
Working with views in Spring MVC applications:
Description: Working with views involves creating views in different formats (JSP, Thymeleaf, etc.) and returning the appropriate view name from controller methods.
Code snippet (Java):
@Controller @RequestMapping("/work") public class WorkController { @GetMapping("/displayInfo") public String displayInfo() { // Returning the view name return "displayInfoView"; } }
Code snippet (Thymeleaf template):
<!-- Thymeleaf template to display information --> <html> <body> <h2 th:text="'Displaying information using Thymeleaf: ' + ${info}"></h2> </body> </html>
Creating and rendering views in Spring MVC controllers:
Description: Creating and rendering views in Spring MVC controllers involves returning the name of the view from the controller method, and Spring resolves this to the actual view.
Code snippet (Java):
@Controller @RequestMapping("/create") public class CreateController { @GetMapping("/showMessage") public String showMessage(Model model) { // Adding data to the model model.addAttribute("message", "Hello, this is a message!"); // Returning the view name return "showMessageView"; } }
Code snippet (JSP - JavaServer Pages):
<!-- JSP view to display a message --> <html> <body> <h2 th:text="${message}"></h2> </body> </html>
Example of building a view in Spring MVC:
Description: An example demonstrating the complete process of building a view in a Spring MVC controller, adding data to the model, and rendering it in a view.
Code snippet (Java):
@Controller @RequestMapping("/example") public class ExampleController { @GetMapping("/showMessage") public String showMessage(Model model) { // Adding data to the model model.addAttribute("message", "This is an example message!"); // Returning the view name return "showMessageView"; } }
Code snippet (JSP - JavaServer Pages):
<!-- JSP view to display a message --> <html> <body> <h2 th:text="${message}"></h2> </body> </html>