Spring Boot 2 With JSP View
Creating a Spring Boot 2 web app just got a lot easier.
Join the DZone community and get the full member experience.
Join For FreeIn this tutorial, I am going to show you how easy it is to create a web application with Spring Boot 2, along with the embedded Tomcat + JSP template
and JSP views.
What You'll Need
1. Project Structure
You can get the blueprint from the Spring Initializer page.
2. Project Dependencies
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>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.6.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.eprogrammerz.examples.spring</groupId>
<artifactId>spring-boot-jsp</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>spring-boot-jsp</name>
<description>Example Spring Boot with JSP view</description>
<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>
<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>
3. Configuration
3.1 Application Configurations
This SpringBootServletInitializer
runs a SpringBootJspApplication
from a traditional WAR deployment
SpringBootJspApplication.java
xxxxxxxxxx
package com.eprogrammerz.examples.spring.springbootjsp;
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;
public class SpringBootJspApplication extends SpringBootServletInitializer {
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(SpringBootJspApplication.class);
}
public static void main(String[] args) {
SpringApplication.run(SpringBootJspApplication.class, args);
}
}
3.2 Resources
application.properties
xxxxxxxxxx
spring.mvc.view.prefix: /WEB-INF/jsp/
spring.mvc.view.suffix: .jsp
4. Controller and View Template
4.1 Create a Controller With Simple Method Handling the Base Request
xxxxxxxxxx
package com.eprogrammerz.examples.spring.springbootjsp.controllers;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
public class HelloController {
"/", "/hello"}) ({
public String hello(Model model, (value="name", required=false, defaultValue="World") String name) {
model.addAttribute("name", name);
return "hello";
}
}
4.2 Add JSP View as a Template
For JSP files, put in src/main/webapp/WEB-INF/jsp/
xxxxxxxxxx
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Hello ${name}!</title>
</head>
<body>
<h2 class="hello-title">Hello ${name}!</h2>
</body>
</html>
5. Run With Maven
You can run your application with a bash command at the project root directory:
xxxxxxxxxx
mvn clean spring-boot:run
Then go to localhost:8080
to test out your application.
That's it!
All code is available on GitHub.
Published at DZone with permission of Yogen Rai, DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.
Comments