On this page, we’ll help you how to start the Hello World application in Spring Boot JSP using Spring Tool Suite. To develop this application, we will use JDK 8 and Maven. Here we will explain step by step procedure with images how to create this application, how to compile and how to run the application in order to see the message “Hello World” in the browser.



Start Spring Boot Hello World Project

Click on the “File “menu in the top left corner-> then click on the “New “menu-> then click on the “Spring Starter Project “button in the image below.

The link above will open a wizard “New Spring Starter Project” as a pop-up window. Change the Name to “SpringHelloWorldJsp”. By default, the location will show in the work space. If you want to change to another location, un-check the “Use default location” location and select the location you want. Change Group, Artifact, Description, Package as shown in the picture. Leave the remaining field as the default value. To move next page, click on the “Next “button.

The window “New Spring Starter Project Dependencies” will open on the next page. There are two list, the “Available” list on the left shows all packages and the “Selected” list on the right is empty. Scroll down the list on the left until the end, expand “Web” from the list “Available”, then select the checkbox “Web”. Now the “Selected” list on the right side shows “Web”. To move next page, click on the “Next” button.

It will show “Site Info” on the next page as shown below. To create a default project, click “Finish” button.



Default Spring Boot Project File Structures

Now it creates the default Spring Boot Project. The directory structures are created as shown in the image below. The file “SpringHelloWorldJspApplication.java” was created in the source folder. This is the main method starting file. A default configuration file called “application.properties” is created in the resource folder. There are two folder named “static” and “templates” created in resources folder. A default test file with default code is created in the test folder “SpringHelloWorldJspApplicationTest.java”. In the root directory, a pom.xml file is created.

The pom.xml file creates dependency of web “spring-boot-starter-web”. Default test dependency is “spring-boot-starter-test”. Add tomcat dependency as shown below “tomcat-embed-jasper”. The pom.xml file will contain the following code.

<?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>
	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>2.1.2.RELEASE</version>
		<relativePath/> <!-- lookup parent from repository -->
	</parent>
	<groupId>com.yawintutor</groupId>
	<artifactId>SpringHelloWorld</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<name>SpringHelloWorldJsp</name>
	<description>Spring Boot Hello World Jsp Project</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-test</artifactId>
			<scope>test</scope>
		</dependency>
		
		<dependency>
			<groupId>org.apache.tomcat.embed</groupId>
			<artifactId>tomcat-embed-jasper</artifactId>
		</dependency>

	</dependencies>

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

</project>

The main Spring boot file “SpringHelloWorldJspApplication.java” is shown below. It contains a main method by default.

package com.yawintutor.helloworld;

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

@SpringBootApplication
public class SpringHelloWorldJspApplication {

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

}

In the test folder, the default test java file “SpringHelloWorldJspApplication.java “contains the default test code.

package com.yawintutor.helloworld;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

@RunWith(SpringRunner.class)
@SpringBootTest
public class SpringHelloWorldJspApplicationTests {

	@Test
	public void contextLoads() {
	}

}


Running the default Spring Boot Application

Open the file “SpringHelloWorldJspApplication.java” in the Project Explorer window by double-clicking on the file name.

You can use the Spring Tool Suite to run the Spring boot application in three ways.

  • First option – Click on the “Run “menu in the menu-> Click on “Run As “-> click on “2. Spring Boot App”
  • Second option – Right click on “SpringHelloWorldJspApplication.java” -> Click on “Run As” -> click on “Run Spring Boot App”
  • Third option- Click in the toolbar on the “Run” button.

The output is displayed in the console window as below. By default, the default log statements are displayed in the console window. Since the tomcat server will start, it will run forever. Click the “stop” button to stop the application.



Create Hello World Jsp Application

Create a java file “HelloWorldController.java” in the package “com.yawintutor.helloworld”. The path is “/src /main /java /com /yawintutor /helloworld /HelloWorldController.java”. As shown below, add the controller code.

Class HelloWorldController contains firstPage() method. This method is invoked when user access from the browser with path “/HelloWorld”. This method returns the object “ModelAndView” with a string “helloworld” that will call helloworld.jsp file.

package com.yawintutor.helloworld;

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

@Controller
public class HelloWorldController {

	@RequestMapping("/HelloWorld")
	public ModelAndView firstPage() {
		return new ModelAndView("helloworld");
	}

}

Open the application.properties file, add the code below and save the file. The first line shows where the jsp files are picked ie, prefix of jsp file. The jsp file suffix is displayed in the second configuration.

spring.mvc.view.prefix:/WEB-INF/jsp/
spring.mvc.view.suffix:.jsp

Create a jsp file “helloworld.jsp” in the “/src/main/webapp/WEB-INF/jsp” folder. (If there is no folder, create as shown in the path). Add the code below in the file “helloworld.jsp” and save.

<h1>Welcome to Hello World!</h1>

The folder structure appears as in the image below. There are two other files “HelloWorldController.java” and “helloworld.jsp”.

Run the application again. The output console window will show as below

Open a browser. In this example, we use the Google Chrome browser. Type http://localhost:8080/HelloWorld, The browser will now display the message “Welcome to HelloWorld!”.

When the application starts, it starts tomcat and listens for request in port 8080. When we call the url in the browser, it connects to the tomcat server with port 8080 and calls the method “firstPage()” in HelloWorldController.java. This method returns with “helloworld”. Using the application.properties file, it appends with both prefix and suffix, calls the jsp file “helloworld.jsp”. This file is displayed in the browser.

On this page, we explained that we started with the Hello World Jsp program in Spring Boot using the Spring Tool Suite.



Leave a Reply