Spring Starter Guide
The following article is a short introduction to the basic setup of a new Spring Web Project.
21. Aug 2020 toc: disabled view: slimInstall Eclipse plugins
The listed Eclipse Plugins are needed to work on Spring projects smoothly.
-
Spring Tools 4
It provides the functionality to create and launch Spring projects.
-
Eclipse Web Developer Tools
Delivers HTML, CSS and JS Editors.
Prepare Eclipse workspace
-
File Association
JSP and Tag files have the same Syntax as HTML, in this case, we can now associate the default HTML Editor (delivered by the Eclipse Web Developer Tools Plugin) also to the file types
.jsp
and.tag
to let Eclipse now with which Editor these files should be opened. -
Perspective and Views
Web Projects are typically viewed in the Resource Perspective, so if the Perspective is not shown by default, add it to your Eclipse Workspace.
The needed views for a smooth working process are the following:
- Project Explorer
- Boot Dashboard
- Console
- Gradle Tasks
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:
-
Name
- The name of your project. -
Type
- The Build Tool to use for the project. In our case, we use Gradle (Buildship 3.x). -
Java
Version - The Java Version for the project. -
Group
- Should be the same as the Package path. -
Artifact
- The name of your startup Java file. -
Version
- The version of your project. -
Description
- The description of your project. -
Package
- The root package of your project.NOTE: All Java files must be found in the defined root package or a subpackage of them, otherwise Spring is unable to locate these files.
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:
-
server.port
Defines which Port is used to listen for incoming requests by the Browser. The default ports are 80 (http://) and 443 (https://).
-
spring.mvc.view.prefix
Defines in which directory the server should looking for them to be served HTML, JSP files. The default path is
/WEB-INF/views/
which represents/src/main/resources/META-INF/resources/WEB-INF/views/
. -
spring.mvc.view.suffix
Defines the file type for them to be served files. The default file type is are
.jsp
and.html
. -
spring.resources.static-locations
Files in the defined path can be modified without restarting the server to see the changes. It is common to put all resources like CSS, JavaScript and Images in
/src/main/resources/static/
.NOTE: This is a temporary property only for development
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.