login

Spring Starter Guide

The following article is a short introduction to the basic setup of a new Spring Web Project.

2020-08-21


Install Eclipse plugins

The listed Eclipse Plugins are needed to work on Spring projects smoothly.

Prepare Eclipse workspace

Create a new project

To create a new Spring project in Eclipse we choose the project type Spring Starter Project under the category Spring Boot.

File > New > Project... > Spring Boot > Spring Starter Project

The following properties are required to set up our project correctly:

Complete the setup wizard by skipping the choose of dependencies and click Finish. Now Eclipse prepares our Spring Project, this could take some time to build up all needed resources.

Next > Next > Finish

Then this is done the worktree should now contain the following directories and files:

Tutorial/
 ├ src/main/
 │  ├ java/
 │  │  └ de.typable.tutorial/
 │  │     └ TutorialApplication.java
 │  └ resources/
 │     └ application.properties
 ├ gradle/
 │  └ ∙∙∙
 ├ build.gradle
 ├ gradlew
 ├ gradlew.bat
 ├ HELP.md
 └ settings.gradle

NOTE: All test directories and files can be deleted. They aren't needed in our working process with Spring.

Building project

The basic workspace setup is completed and we can now define the essential dependencies for Spring Web.

All dependencies for this project are managed by a Build Tool, in this case, Gradle.

The dependencies for our project should be referenced in the build.gradle file. For that replace the content of the /build.gradle file with the following code snippet:

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

/* Project configurations */
group = 'de.innovate.tutorial'
version = '0.0.1'
sourceCompatibility = '11'

/* Dependency download service */
repositories {
  mavenCentral()
}

/* Required dependencies */
dependencies {
  /* Spring Web */
  implementation 'org.springframework.boot:spring-boot-starter-web'

  /* JSP & Taglibs */
  implementation 'org.apache.tomcat.embed:tomcat-embed-jasper:9.0.27'
  implementation 'javax.servlet:jstl:1.2'
}

We have now referenced the elementary dependencies for a Spring Web project. Additional could be added in the dependencies section.

The next step is to build the project with Gradle to download all in the build.gradle defined dependencies. To do that go to the Gradle Tasks View and double-click on build under the build section.

Gradle Tasks > {Project} > build > build

During the building process, the current progress will be displayed in the Gradle Executions View and the Console View. The Gradle Executions View is in most cases needless because the Console gives us much clearer feedback about what's happening and what went wrong if the build crashes.

To ensure the building process is successful the Console displays the message BUILD SUCCESSFUL.

NOTE: Disabling the Gradle Executions View can be done as followed: Window > Preferences > Gradle > Show Executions View

The building process is finished and this has affected our project worktree, because of that, we need to refresh our project. For that, we right-click on the project in the Package Explorer View and click on Refresh Gradle Project under the Gradle section. After that, our Spring project is up to date.

Project Explorer > {Project} > Gradle > Refresh Gradle Project

Configuration

To optimize the project to our needs, we configure some properties in the application.properties file. This can also be done with XML.

Add the following properties to /src/main/resources/application.properties:

server.port=8080
spring.mvc.view.prefix=/WEB-INF/views/
spring.mvc.view.suffix=.jsp
# Reload static resources (temporary)
spring.resources.static-locations=file:src/main/resources/static/

Here a short explanation of the properties from above:

Launch

Now it's at the time to launch our Spring Web server to confirm all is configured correctly.

To start the Apache Tomcat server that provides the basic functionality of a web server which is integrated in our handy Spring Tools Plugin, we go to the Boot Dashboard View expand the local section and select the project with our predefined name and click on the "Start or restart" button at the top of the View.

To confirm the server is started correctly the console should show up the message: Started TutorialApplication ...

Now the Spring Web server is available under the defined port and can be viewed in the Browser of your choice. The page should show the message Whitelabel Error Page, this means that no content is currently available.

Now the Spring setup is complete and you can begin with your custom implementation.