spring-boot-starter-thymeleaf – needed for thymeleaf
pom.xml
<?xml version="1.0" encoding="UTF-8"?><projectxmlns="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>2.7.14</version><relativePath/><!-- lookup parent from repository --></parent><groupId>com.spring.mvc.config</groupId><artifactId>mvc</artifactId><version>0.0.1-SNAPSHOT</version><name>mvc</name><description>Spring MVC Application Using Spring Boot and Thymeleaf</description><properties><java.version>11</java.version></properties><dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-thymeleaf</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency></dependencies><build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId></plugin></plugins></build></project>
Thymeleaf is a default template used by Spring Boot. By default, Spring Boot looks for the templates in src/main/resources/templates location.
Add the html files inside src/main/resources/templates
landing.html
landing.html
<htmlxmlns:th=″http://www.thymeleaf.org″><head><metahttp-equiv="Content-Type"content="text/html; charset=UTF-8"><title>Application</title></head><body><h3>Spring MVC Application using Spring Boot and Thymeleaf</h3><br/><br/><divclass="form"><formaction="welcome"method="post"><table><tr><td>Please Enter Your Name</td><td><inputid="name"name="name"></td><td><inputtype="submit"value="Submit"></td></tr></table></form></div></body></html>
welcome.html
welcome.html
<htmlxmlns:th=″http://www.thymeleaf.org″><head><metahttp-equiv="Content-Type"content="text/html; charset=UTF-8"><title>Application</title></head><body><h3>Spring MVC Application using Spring Boot and Thymeleaf</h3><br/><br/><h2>Entered name is [[${name}]]</h2><divclass="form"><formaction="/"method="get"><table><tr><td><inputtype="submit"value="Go back"></td></tr></table></form></div></body></html>
Running Application
Execute the main method of Application.class and then hit http://localhost:8080
Enter name and click submit button
Click Go back button. It will load the landing page.