Spring Framework
The following article is a short introduction to the basic setup of a new Spring Web Project. All required dependencies for this project are managed by a Build Tool, in this case, Gradle.
Requirements
Install Eclipse Plugins
The listed Eclipse Plugins are needed to work on Spring projects smoothly. To check if they are already installed and if not how to install them, check out this article: Eclipse - Install Plugins
-
Spring Tools 4 - It provides the functionality to create and launch Spring projects.
Eclipse Marketplace -
Eclipse Web Developer Tools - Deliveres HTML, CSS and JS Editors.
Eclipse Marketplace
Prepare Eclipse Workspace
-
File Association
"JSP and Tag files has 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.
More details about the association of file types could be found in this article: Eclipse - Manage File Associations -
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
More about managing Perspectives and Views can be found here: Eclipse - Manage Perspectives and Views
Getting Started
Project Setup
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. The currently preferred Version is 11.
-
Group - Should be the same as the Package path.
-
Artifact - The name of your startup Java file.
-
Version - The version of your project.
-
Description - A short description of your project.
-
Package - The root package of your project.
Important: 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.
Then this is done the worktree should now contain the following directories and files:
Tutorial
├ src/main
| ├ java
| | └ de.innovate.tutorial
| | └ TutorialApplication.java
| └ resources
| └ application.properties
├ gradle
| └ ∙∙∙
├ build.gradle
├ gradlew
├ gradlew.bat
├ HELP.md
└ settings.gradle
Notice: All test directories and files can be deleted. They aren't needed in our working process with Spring.
Project Building
The basic workspace setup is completed and we can now define the essential dependencies for Spring Web. All required 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'
}
group = 'de.typable.tutorial'
version = '0.0.1'
sourceCompatibility = '11'
repositories {
mavenCentral()
}
dependencies {
/* Spring Web */
implementation 'org.springframework.boot:spring-boot-starter-web'
/* JSP & JSTL */
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. More about dependencies and where to find them could be found in this article: Gradle - Dependencies
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.
Tip: 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
Configurations
To optimize the project to our needs, we configure some properties in the application.properties file. This can also be done with XML. More information about XML configuration can be found here: Spring - XML Configurations
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://).
Example: 80 -> http://localhost, 443 -> https://localhost, 8080 -> http://localhost:8080 -
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/.
Notice: This is a temporary property only for development
Start Up
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 everything is ready to build your project.