Create a Web Application With Spring Boot
ADVERTISEMENT
Table of Contents
- Introduction
- 1. Create maven web project
- 2. pom.xml
- 3. Application.java
- 4. application.properties
- 5. home.jsp
- 6. HomeController.java
- 7. Deploy the application
- Summary
- Next Steps
Introduction
This guide shows how to create a simple MVC web application using Spring Boot.
Prerequisites:
- Eclipse IDE (neon release)
- Maven 4
- Java 1.8
1. Create maven web project
Open eclipse then create a new maven web project and name it as SpringBootWebJsp.
The structure of the generated projects look like the following:
2. pom.xml
After creating the web project, the first step is to configure Spring Boot inside pom.xml, so we add the following as a parent dependency:
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.10.RELEASE</version>
</parent>
Spring Boot exposes a starter called spring-boot-starter-web which automatically imports all the required jars needed to setup a typical Spring MVC application and automatically configures the view resolver and the servlet dispatcher of the application so that the developer focuses on the development rather than the configuration, so we add the starter as a dependency:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
Since we’re using JSP as the front end technology, we need to add the following dependency in order to be able to compile JSP and make use of its features:
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
<scope>provided</scope>
</dependency>
That’s all, just 2 dependencies can make your MVC application up. Below are the auto-imported jars:
This is the whole pom.xml for reference:
<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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.programmer.gate</groupId>
<artifactId>SpringBootWebJSP</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>SpringBootWebJSP</name>
<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.10.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>
<scope>provided</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
P.S: when using JSP as a front end technology, you should make sure to set the “packaging” attribute as war not jar, since Spring Boot team claimed that currently there is limitations for supporting JSP inside jar file ( the view resolver wouldn’t map correctly).
3. Application.java
The second step is to create the Spring Boot initializer class, this is the entry point of our application. When annotating a class with @SpringBootApplication, we’re configuring our application to run over the embedded servlet container provided by Spring Boot (tomcat by default).
package com.programmer.gate;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class Application{
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
4. application.properties
Spring Boot auto configures the view resolver and the dispatcher servlet of the web application and provides us a way to define our own configuration using application.properties.
So we create application.properties under src/main/resources and define the following properties:
spring.mvc.view.prefix=/WEB-INF/jsp/
spring.mvc.view.suffix=.jsp
server.port=9093
home.message= Programmer Gate
Here we’re configuring the view resolver to map the views to files of type jsp under “WEB-INF/jsp/”. We also change the default port of the embedded tomcat to be 9093, as well as defining other business message properties to be used later on inside our application.
5. home.jsp
In this tutorial, we’re creating a very simple MVC application which displays a welcome message to the end user, so we create a very basic jsp file called home.jsp under WEB-INF/jsp:
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
</head>
<body>
<h1>Welcome to ${message} </h1>
</body>
</html>
6. HomeController.java
Now we create our main controller named HomeController under com.programmer.gate and we define it to serve requests coming from the root path as the following:
package com.programmer.gate;
import java.util.Map;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class HomeController {
@Value("${home.message}")
private String message;
@RequestMapping("/")
public String welcome(Map<String, Object> model) {
model.put("message", this.message);
return "/home";
}
}
Our controller simply reads the welcome message from application.properties and then redirects to home.jsp.
7. Deploy the application
Following are the steps to deploy our application:
- Right click pom.xml -> run-as -> Maven install
- Maven generates a war file called SpringBootWebJSP-0.0.1-SNAPSHOT.war inside target folder
- Open cmd, then run the war using: java -jar SpringBootWebJSP-0.0.1-SNAPSHOT.war
Here we go, our application is up and ready to serve requests at port 9093.
That’s it, I hope you like it. For clarifications please leave your thoughts in the comments section below.
Summary
This guide shows how to create a simple MVC web application using Spring Boot.
Next Steps
If you're interested in learning more about the basics of Java, coding, and software development, check out our Coding Essentials Guidebook for Developers, where we cover the essential languages, concepts, and tools that you'll need to become a professional developer.
Thanks and happy coding! We hope you enjoyed this article. If you have any questions or comments, feel free to reach out to jacob@initialcommit.io.
Final Notes
Recommended product: Coding Essentials Guidebook for Developers