Tutorial describing how to create the simple Hello World application using Spring Boot and JSP.
Technologies used:
- Spring Boot 2.1.7.RELEASE
- JDK 1.8
- Maven 3
- JSP
Project setup
Project dependencies managed by the Maven’s pom.xml configuration file:
<?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>2.1.7.RELEASE</version> <relativePath/> </parent> <groupId>com.devcases.springboot</groupId> <artifactId>springboot-helloworld-jsp</artifactId> <version>1.0.0-SNAPSHOT</version> <packaging>war</packaging> <properties> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-tomcat</artifactId> <scope>provided</scope> </dependency> <dependency> <groupId>org.apache.tomcat.embed</groupId> <artifactId>tomcat-embed-jasper</artifactId> <scope>provided</scope> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
The explanation of the above dependencies:
spring-boot-starter-web
– packages all the necessary dependencies together with the auto configuration for running simple web applicationspring-boot-starter-tomcat
– provides the embedded tomcat application servertomcat-embed-jasper
– enables support for JSPspring-boot-maven-plugin
– plugin prividing commands to work with a Spring Boot application (we will be usine thespring-boot:run
command)
Application Initializer
The standard spring boot application initializer, which our application need to provide:
@SpringBootApplication
public class Application extends SpringBootServletInitializer {
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(Application.class);
}
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
Controller
The controller containing rest services:
@Controller
public class HelloWorldController {
private static final String message = "Hello World";
@GetMapping(value = "/")
public String hello(Map<String, Object> model) {
model.put("message", message);
return "hello";
}
}
The controller has only one endpoint available at the root address (/
– in our example it is resolved to the localhost:8080
) . The method passes the message
parameter to the hello
view.
View
<!DOCTYPE html> <%@ taglib prefix="spring" uri="http://www.springframework.org/tags"%> <html lang="en"> <head> <meta charset="UTF-8"> <title>${message}!</title> </head> <body> <h2>${message}!</h2> </body> </html>
The view contains a simple html page, that shows passed parameter.
Configuration
The configuration in application.properties
file contains information how to resolve JSP files.
spring.mvc.view.prefix: /WEB-INF/jsp/
spring.mvc.view.suffix: .jsp
Running
The project can be started by the running the following command:
mvn clean spring-boot:run
The command deploys the apllication on the embedded tomcat server.
By visiting the localhost:8080
url, we should see the page containg the Hello World message.
The example code you can download from here