Spring Boot Web Application Example

Spring Boot Web Application

Spring Boot Web Application Example

In this tutorial, I will show you how to create web applications using Spring Boot framework.

For developing a web application in Spring Boot framework, you should have at least basic understanding of Web Development and Spring MVC framework. You can find good tutorials here :
Spring MVC :  Spring MVC Introduction.
Spring Boot framework basics : Spring Boot | Getting Started 

So, let’s get started.

Create a Maven Web Application Project

First, we need to create a maven project using web app archetype. We are going to use Spring Tool Suite for this tutorial :

1. Go to File -> Create New Maven Project

Spring Boot Web Application



2. Click Next then select maven archetype web app.

Spring Boot Web Application



3. Click next and provide details as below and click finish.

Spring Boot Web Application



4. Create the files and folder as per the given screen shot :

Spring Boot Web Application




Provide dependencies in Maven configuration file (pom.xml)

Maven is useful and most power tool to manage dependencies in your application. You can provide all the dependency details in pom.xml file.

<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.springbootweb</groupId>
 <artifactId>SpringBootWeb</artifactId>
 <packaging>war</packaging>
 <version>0.0.1-SNAPSHOT</version>
 <name>SpringBootWeb Maven Webapp</name>
 <url>http://maven.apache.org</url>

 <parent>
 <groupId>org.springframework.boot</groupId>
 <artifactId>spring-boot-starter-parent</artifactId>
 <version>1.4.7.RELEASE</version>
 </parent>

 <properties>
 <java.version>1.7</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>

Define Controller

Controller is mainly used to handle client requests and responses in web applications. We can define controller by simply using @Controller annotation which is the mark that this java file is a controller configuration file. Container will then look for the controller files which have @Controller annotation.

IndexController.java
package com.springbootweb.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class IndexController {

 @RequestMapping("/")
 public String indexPage() {
 return "index";
 }
 
}

Create Application Property file

Application Property file is useful to define some static contents which can be used by application at run time like data base configuration, static messages, application settings and much more so that you don’t have to restart the application everytime in order to perform little changes.

Create this file under resources folder of maven application.

application.properties
spring.mvc.view.prefix: /WEB-INF/views/
spring.mvc.view.suffix: .jsp
server.port = 7474

Create Spring Boot Application runner

Now we will create a separate file which automatically configures the initial configuration for you and starts the application for you. So, you don’t have to manually configure each step on your own like tomcat server, web application xml, spring xml, servlet initialization etc.


WebApplication.java
package com.app;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.context.annotation.ComponentScan;

@SpringBootApplication
@ComponentScan(basePackages = {"com.springbootweb.controller"})
public class WebApplication {

 public static void main(String[] args) throws Exception {
 SpringApplication.run(WebApplication.class, args);
 }

}


Create Simple JSP file

The JSP file is only used to display useful information to the clients which is sometimes called the view file. We will create view file under WEB-IN/view folder as below.

index.jsp
<html>
<body>
<h2>Welcome to the Spring Boot web application.</h2>
</body>
</html>

Run the Application

Now you are ready to run the application : Select the project -> Right click on it -> Run as -> Spring Boot App.

You will see the output as below :

Spring Boot Web Application

Related Post

Leave a Reply

Your email address will not be published. Required fields are marked *