In this article, we will learn how to create a Spring Boot MVC application, which is simply a Spring MVC application using Spring Boot. Unlike a typical Spring Boot application, we will be rendering the JSP page to the user. Let’s get started.
Folder Structure:
- Create a Maven project (maven-archetype-quickstart) “SpringBootExample” and create a package for our source files “com.javainterviewpoint” under src/main/java
- Now add the following dependency in the POM.xml
<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 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.javainterviewpoint</groupId> <artifactId>SpringBootExample</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>war</packaging> <name>SpringBootExample</name> <url>http://maven.apache.org</url> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <java.version>1.8</java.version> </properties> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.1.0.RELEASE</version> </parent> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.apache.tomcat.embed</groupId> <artifactId>tomcat-embed-jasper</artifactId> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
- Create the Java classes App.java and HelloController.java under com.javainterviewpoint folder.
- Being an MVC application, we need to create a folder to place our JSP files, in our case it is WEB-INF/view folder and place the hello.jsp file.
- Create application.properties file under src/main/resources directory and provide the below entries which is need for Spring MVC
# Spring MVC settings spring.mvc.view.prefix: /WEB-INF/view/ spring.mvc.view.suffix: .jsp
The spring-boot-starter-parent is a special starter, it provides useful Maven defaults. Since we are developing a web application we also need to add spring-boot-starter-web dependency.This will add dependencies such Tomcat, Jackson, Spring boot etc which is required to create a web app.
Also note that we have newly added tomcat-embed-jasper as dependency this will provide support for rendering .jsp pages and this is a must for Spring Boot MVC application.
Spring Boot MVC | Spring Boot JSP Example
Dependency Tree
[INFO] --------------< com.javainterviewpoint:SpringBootExample >-------------- [INFO] Building SpringBootExample 0.0.1-SNAPSHOT [INFO] --------------------------------[ war ]--------------------------------- [INFO] [INFO] --- maven-dependency-plugin:3.1.1:tree (default-cli) @ SpringBootExample --- [INFO] com.javainterviewpoint:SpringBootExample:war:0.0.1-SNAPSHOT [INFO] +- org.springframework.boot:spring-boot-starter-web:jar:2.1.6.RELEASE:compile [INFO] | +- org.springframework.boot:spring-boot-starter:jar:2.1.6.RELEASE:compile [INFO] | | +- org.springframework.boot:spring-boot:jar:2.1.6.RELEASE:compile [INFO] | | +- org.springframework.boot:spring-boot-autoconfigure:jar:2.1.6.RELEASE:compile [INFO] | | +- org.springframework.boot:spring-boot-starter-logging:jar:2.1.6.RELEASE:compile [INFO] | | | +- ch.qos.logback:logback-classic:jar:1.2.3:compile [INFO] | | | | +- ch.qos.logback:logback-core:jar:1.2.3:compile [INFO] | | | | \- org.slf4j:slf4j-api:jar:1.7.26:compile [INFO] | | | +- org.apache.logging.log4j:log4j-to-slf4j:jar:2.11.2:compile [INFO] | | | | \- org.apache.logging.log4j:log4j-api:jar:2.11.2:compile [INFO] | | | \- org.slf4j:jul-to-slf4j:jar:1.7.26:compile [INFO] | | +- javax.annotation:javax.annotation-api:jar:1.3.2:compile [INFO] | | +- org.springframework:spring-core:jar:5.1.8.RELEASE:compile [INFO] | | | \- org.springframework:spring-jcl:jar:5.1.8.RELEASE:compile [INFO] | | \- org.yaml:snakeyaml:jar:1.23:runtime [INFO] | +- org.springframework.boot:spring-boot-starter-json:jar:2.1.6.RELEASE:compile [INFO] | | +- com.fasterxml.jackson.core:jackson-databind:jar:2.9.9:compile [INFO] | | | +- com.fasterxml.jackson.core:jackson-annotations:jar:2.9.0:compile [INFO] | | | \- com.fasterxml.jackson.core:jackson-core:jar:2.9.9:compile [INFO] | | +- com.fasterxml.jackson.datatype:jackson-datatype-jdk8:jar:2.9.9:compile [INFO] | | +- com.fasterxml.jackson.datatype:jackson-datatype-jsr310:jar:2.9.9:compile [INFO] | | \- com.fasterxml.jackson.module:jackson-module-parameter-names:jar:2.9.9:compile [INFO] | +- org.springframework.boot:spring-boot-starter-tomcat:jar:2.1.6.RELEASE:compile [INFO] | | \- org.apache.tomcat.embed:tomcat-embed-websocket:jar:9.0.21:compile [INFO] | +- org.hibernate.validator:hibernate-validator:jar:6.0.17.Final:compile [INFO] | | +- javax.validation:validation-api:jar:2.0.1.Final:compile [INFO] | | +- org.jboss.logging:jboss-logging:jar:3.3.2.Final:compile [INFO] | | \- com.fasterxml:classmate:jar:1.4.0:compile [INFO] | +- org.springframework:spring-web:jar:5.1.8.RELEASE:compile [INFO] | | \- org.springframework:spring-beans:jar:5.1.8.RELEASE:compile [INFO] | \- org.springframework:spring-webmvc:jar:5.1.8.RELEASE:compile [INFO] | +- org.springframework:spring-aop:jar:5.1.8.RELEASE:compile [INFO] | +- org.springframework:spring-context:jar:5.1.8.RELEASE:compile [INFO] | \- org.springframework:spring-expression:jar:5.1.8.RELEASE:compile [INFO] \- org.apache.tomcat.embed:tomcat-embed-jasper:jar:9.0.21:compile [INFO] +- org.apache.tomcat.embed:tomcat-embed-core:jar:9.0.21:compile [INFO] | \- org.apache.tomcat:tomcat-annotations-api:jar:9.0.21:compile [INFO] +- org.apache.tomcat.embed:tomcat-embed-el:jar:9.0.21:compile [INFO] \- org.eclipse.jdt:ecj:jar:3.16.0:compile [INFO] ------------------------------------------------------------------------
App.java with SpringBootServletInitializer
SpringBootServletInitializer is an abstract class implementing WebApplicationInitializer Interface. We have extended the SpringBootServletInitializer class and overridden the configure() method of it, this enables our application configurable when launched by any Traditional Web Container.
@SpringBootApplication annotation does the work of @EnableAutoConfiguration, @Configuration and @ComponentScan annotations together
package com.javainterviewpoint; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.builder.SpringApplicationBuilder; import org.springframework.boot.web.servlet.support.SpringBootServletInitializer; @SpringBootApplication public class App extends SpringBootServletInitializer { @Override protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) { return builder.sources(App.class); } public static void main(String[] args) { SpringApplication.run(App.class); } }
HelloController
package com.javainterviewpoint; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.GetMapping; @Controller public class HelloController { @GetMapping({"/", "/hello"}) public String showWelcomePage() { return "hello"; } }
@GetMapping maps “/” and “/hello” request to showWelcomePage() method, which redirects us to the view page “hello”(hello.jsp)
Resolving JSP pages
There are two approaches which can be followed to resolve the JSP pages
1. Using the application.properties
This is the simplest way for resolving the JSP files, all you need to do is to add the below two entries in the application.properties file and Spring Boot will take care of the rest.
# Spring MVC settings spring.mvc.view.prefix: /WEB-INF/view/ spring.mvc.view.suffix: .jsp
2. Using the configuration file
In this approach, we will have to manually define the View Resolver which needs to be used while resolving the JSP pages
package com.javainterviewpoint; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.web.servlet.config.annotation.EnableWebMvc; import org.springframework.web.servlet.view.InternalResourceViewResolver; @Configuration @EnableWebMvc public class SpringConfig { @Bean public InternalResourceViewResolver viewResolver() { InternalResourceViewResolver viewResolver = new InternalResourceViewResolver(); viewResolver.setPrefix("/WEB-INF/view/"); viewResolver.setSuffix(".jsp"); return viewResolver; } }
hello.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> <!DOCTYPE html> <html> <head> <meta charset="ISO-8859-1"> <title>Insert title here</title> </head> <body> <h3>Welcome to Spring Boot MVC</h3> </body> </html>
Output:
Select the Project –>Run As –> Run Configuration –>Maven –> New. In the Main tab, key in the Goals as “spring-boot:run” and click on Run.
Now hit on the URL http://localhost:8080
Things to Remember:
- Don’t forget to add “tomcat-embed-jasper” jar as dependency, this enables the Spring Boot application to render JSP files.
- Make sure to change the packaging to WAR [<packaging>war</packaging>] in the pom.xml
- Place the jsp files under /WEB-INF/view directory
- Add the mvc related entries in the application.properties
- spring.mvc.view.prefix: /WEB-INF/view/
- spring.mvc.view.suffix: .jsp
Leave a Reply