Spring Boot first web application

In this tutorial, we are going to create a simple Spring Boot web application. We will use NetBeans to build the application and the application is going to be deployed on Tomcat server.

Spring is a popular Java application framework. Spring Boot is an effort to create stand-alone, production-grade Spring based applications with minimal effort.

First, we create a new web application in NetBeans IDE.

Spring Boot web project structure in NetBeans
Figure: Spring Boot web project structure in NetBeans

This is the project structure of the Spring Boot web application in NetBeans.

context.xml
<?xml version="1.0" encoding="UTF-8"?>
<Context path="/SpringBootWebFirstEx"/>

This is the context.xml file. It is used to uniquely identify the application.

pom.xml
<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.zetcode</groupId>
    <artifactId>SpringBootWebFirstEx</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>war</packaging>
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <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.4.2.RELEASE</version>
    </parent>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
    </dependencies>
</project>

This is the Maven build file. The spring-boot-starter-web is starter for building web, including RESTful, applications using Spring MVC. The application is packaged into a WAR file, which is going to be deployed on Tomcat.

application.properties
spring.mvc.view.prefix: /WEB-INF/jsp/
spring.mvc.view.suffix: .jsp
application.message: Hello there

The application.properties file contains various configuration settings of a Spring Boot application. The first two properties set the prefix and suffix of the Spring MVC module. The third is a custom message option.

Application.java
package com.zetcode.web;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.support.SpringBootServletInitializer;

@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);
    }
}

The Application sets up the Spring Boot application. The SpringBootServletInitializer runs a Spring application from the traditional WAR deployment.

MyController.java
package com.zetcode.web;

import java.util.Date;
import java.util.Map;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;

@Controller
public class MyController {
      
    @Value("${application.message}")
    private String message = "Hi there";

    @GetMapping("/")
    public String index(Map<String, Object> model) {
        
        model.put("date", new Date());
        model.put("message", this.message);
        return "index";
    }
}

This is the controller class for the Spring Boot web application. A controller is decorated with the @Controller annotation. The controller has one mapping. The mapping resolves to the index.jsp, which is located in the WEB-INF/jsp directory.

@Value("${application.message}")
private String message = "Hi there";

We inject a value from the application.properties into the message variable.

@GetMapping("/")
public String index(Map<String, Object> model) {
    
    model.put("date", new Date());
    model.put("message", this.message);
    return "index";
}

The GetMapping annotation maps a GET request with the / path to the index method handler. A model is created and filled with data. The model is a Map interface, which allows for the complete abstraction of the view technology. The model Map is simply transformed into an appropriate format, such as JSP request attributes.

index.jsp
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
    <head>
        <title>JSP Page</title>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">        
    </head>
    <body>
        <p>Date: ${date}</p>
        <p>Message: ${message}</p>
    </body>
</html>

The index.jsp displays two values: the current date and the received message. Both values are passed to the JSP via the controller.

Spring Boot first web example
Figure: Spring Boot first web example

The application is deployed on the NetBeans' built-in Tomcat server, which listens on port 8084.

In this tutorial, we have created our first Spring Boot web application. You might also be interested in the related tutorials: Introduction to Spring web applications, Standalone Spring applications, FreeMarker tutorial, Java tutorial, Introduction to Play, Introduction to Spark, or Introduction to Stripes.