Built and Run the example

In this example, we are using the web-based Spring Initializr interface to generate the package. Here we have added web dependency only to demonstrate the running, building jar/war and understanding build.gradle / pom.xml

Step 1: Open https://start.spring.io to initialize your project with your selected dependencies. Here we are using 'Spring Web', 'Thymeleaf', and 'Spring Boot DevTools' dependencies for the hello world demonstration. 

 

Now, click on the generate button, it will start downloading a demo.zip file.

Step 2: Extract the downloaded zip in step 1. Import this project to your favorite IDE by navigating the extracted folder path and selecting the pom.xml or build.gradle file, you will get pom.xml when your project is a Maven project and build.gradle if your project is a Gradle project. Here we will be using IntelliJ Idea. 

 

 

 

  

Click on OK and follow the instructions with default settings.

On successful importing, you will get the main runner class DemoApplication.java 

	package com.example.demo;

	import org.springframework.boot.SpringApplication;
	import org.springframework.boot.autoconfigure.SpringBootApplication;

	@SpringBootApplication
	public class DemoApplication {

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

	}

 

If you selected Maven project then you will get pom.xml 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.2.6.RELEASE</version>
		<relativePath/> <!-- lookup parent from repository -->
	</parent>
	<groupId>com.example</groupId>
	<artifactId>demo</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<name>demo</name>
	<description>Demo project for Spring Boot</description>

	<properties>
		<java.version>1.8</java.version>
	</properties>

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

		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-devtools</artifactId>
			<scope>runtime</scope>
			<optional>true</optional>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
			<scope>test</scope>
			<exclusions>
				<exclusion>
					<groupId>org.junit.vintage</groupId>
					<artifactId>junit-vintage-engine</artifactId>
				</exclusion>
			</exclusions>
		</dependency>
	</dependencies>

	<build>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
			</plugin>
		</plugins>
	</build>

</project>

 

And if you have selected Gradle project then you will get build.gradle file.

plugins {
	id 'org.springframework.boot' version '2.2.6.RELEASE'
	id 'io.spring.dependency-management' version '1.0.9.RELEASE'
	id 'java'
}

group = 'com.example'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '1.8'

configurations {
	developmentOnly
	runtimeClasspath {
		extendsFrom developmentOnly
	}
}

repositories {
	mavenCentral()
}

dependencies {
	implementation 'org.springframework.boot:spring-boot-starter-thymeleaf'
	implementation 'org.springframework.boot:spring-boot-starter-web'
	developmentOnly 'org.springframework.boot:spring-boot-devtools'

	testImplementation('org.springframework.boot:spring-boot-starter-test') {
		exclude group: 'org.junit.vintage', module: 'junit-vintage-engine'
	}
}

test {
	useJUnitPlatform()
}

 

Step 3: Now its time to write your application logic. Add a HelloController.java in package com.example.demo.

 

package com.example.demo;

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

@Controller
public class HelloController {

    @RequestMapping("/hello")
    public String helloPage(Model model){
        String s = "Hello World Example";
        model.addAttribute("var1",s);
        return "hello";
    }
}

 Note: Here we have used

          @Controller to make the HelloController Class to behave as a request controller.

          @RequestMapping to name the request URL and hello page is returning String "hello" means Thymeleaf template engine will search for hello.html

file.

Step 4: Add hello.html in resources/templates/  folder.

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <title>Spring Boot Example</title>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css">
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.16.0/umd/popper.min.js"></script>
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.4.1/js/bootstrap.min.js"></script>
</head>
<body>
<div class="container">
    <div class="jumbotron">
        <h1>Spring Boot Tutorials</h1>
        <p th:text="${var1}"></p>
    </div>
    <p><strong>Easytutorials.live</strong> provides simple and comprehensive tutorials on spring-boot, thymeleaf and linux.</p>
    <p>visit  <a href="http://easytutorials.live">easytutorials.live</a> .</p>
</div>

</body>
</html>

 Note: Here we have used html template from w3school bootstrap 4 example.

<p th:text="${var1}"></p> is used to write the object's value inside paragraph tag defined in the helloPage method of HelloController.java 

 

Step 5: Now its turn to run this example by clicking on Run button from IDE or you can run from Termial/cmd, go to the projects directory.

  If you are Linux user and using terminal then run the following command:

     $ ./mvnw spring-boot:run for Maven project

     $ ./gradlew bootRun for Gradle project

  If you are a Windows user and using cmd then run the following command:

     $ mvnw.cmd spring-boot:run for Maven project

     $ gradlew.bat bootRun for Gradle project

 

 



×