Spring Framework logo by Pivotal Software is licensed under Apache License 2.0
In Java Spring, ModelAndView is a class that represents both a model and a view for a web application. It is typically used in the context of Spring MVC (Model-View-Controller) to handle web requests and provide a way to pass data to a view template for rendering.
Using ModelAndView allows you to separate the model data and the view template, making your code more modular and following the MVC pattern, which is a common architectural pattern in web development. Note that in more recent versions of Spring, you can often return a Map
I added thymeleaf, bootstrap and jquery as dependencies
pom.xml
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>3.1.5</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.haddley</groupId> <artifactId>springdatajpa</artifactId> <version>0.0.1-SNAPSHOT</version> <name>springdatajpa</name> <description>Demo project for Spring Boot</description> <properties> <java.version>17</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jpa</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-validation</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> <scope>runtime</scope> <optional>true</optional> </dependency> <dependency> <groupId>com.h2database</groupId> <artifactId>h2</artifactId> <scope>runtime</scope> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <optional>true</optional> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency> <!-- https://mvnrepository.com/artifact/org.webjars/bootstrap --> <dependency> <groupId>org.webjars</groupId> <artifactId>bootstrap</artifactId> <version>3.4.1</version> </dependency> <!-- https://mvnrepository.com/artifact/org.webjars/jquery --> <dependency> <groupId>org.webjars</groupId> <artifactId>jquery</artifactId> <version>3.6.0</version> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <configuration> <excludes> <exclude> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> </exclude> </excludes> </configuration> </plugin> </plugins> </build> </project>
I added a thymeleaf page (as an alternative to JSP)
index.html
<!DOCTYPE html> <html xmlns:th="http://www.thymeleaf.org"> <head> <meta charset="utf-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1"> <link th:rel="stylesheet" th:href="@ " /> </head> <body> <div class="container"> <h1 class="text-center">Todo List</h1> <hr /> <p th:text="|Happy $|">Today</p> <table class="table table-bordered table-striped"> <thead> <tr> <th>Actions</th> <th>Id</th> <th>Description</th> <th>Complete</th> <th>Created Date</th> <th>Modified Date</th> </tr> </thead> <tbody> <tr th:each="item : $" th:class="$ ? success : warning"> <td> <div class="btn btn-group-sm" role="group"> <a class="btn btn-info" th:href="@(id=$)}">Edit</a> <a class="btn btn-danger" th:href="@(id=$)}">Delete</a> </div> </td> <td th:text="$">item_id</td> <td th:text="$">item_description</td> <td th:text="$">item_complete</td> <td th:text="$">item_created_date</td> <td th:text="$">item_modified_date</td> </tr> </tbody> </table> <p><a class="btn btn-success" href="/create-todo">Add a Todo!</a></p> </div> <script th:src="@"></script> <script th:src="@"></script> </body> </html>
I updated the controller to populate the fetch the todo items and to pass them and the current day of the week to the thymeleaf view.
The new home page displays the todo items
I updated the controller to handle a delete request
SpringdatajpaApplication.java
I clicked the Delete button on the second todo item
The second todo item has been deleted