Very basic web application with Spring Boot and Eclipse STS
This post show you how to build a basic web application with Spring MVC + Spring Boot using Spring Tool Suite (STS).
First will be created a base starting project from Eclipse, then a very simple controller will be added returning a string as view content, and finally will be show how to serve an html file from the controller.
Create the project in Eclipse
- Open Eclipse STS.
- Right click on the project panel (or go on Menu > File) then New > Spring Starter Project.
- Fill the following fields in the form:
- Name: the eclipse project name (e.g. “spring-boot-basewebapp”).
- Group: a group name for your projects (e.g. “netgloo”).
- Artifact: the name of the jar file (e.g. “spring-boot-basewebapp”).
- Description: a brief human readable description (e.g. “Spring Boot base Web Application”).
- Package name: the java root package name (e.g. “netgloo”).
Note: Group and Artifact are from Maven, and here there is a brief explanation if you want to follow their conventions: http://maven.apache.org/guides/mini/guide-naming-conventions.html.
- Click Next. Search and select the “Web” dependency, then click Finish.
- Will be downloaded a basic project template from the Spring web site.
Now you should get a project structure like this:
basewebapp/
+ src/
| + main/
| | + java/
| | | + netgloo/
| | | | - Application.java
| | + resources/
| | | | - application.properties
| + main/
| + test/
+ target/
- pom.xml
Sample code
- Create a new folder “controllers” in “src/main/java/netgloo” and add a java class “MainController” inside that folder:
src/main/java/netgloo/controllers/MainController.java
containing the following code:
package netgloo.controllers; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody; @Controller public class MainController { @RequestMapping("/") @ResponseBody public String index() { return "Hello World!!!"; } }
- Right click on the project folder > Run As… > Spring Boot App
- Open your Web Browser and go to http://localhost:8080/. You should see the text “Hello World!!!” returned by the
index
method.
Serving an html page
- Create a new folder “static” inside “src/main/resources” and create a file “hello.html”:
src/main/resources/static/hello.html
containing:
<!DOCTYPE html> <html> <body> <h2>Hello world!</h2> </body> </html>
- Remove the
@ResponseBody
annotation and specify the file that will be returned (“hello.html”) on the controller classBasicController
:package netgloo.controllers; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; @Controller public class BasicController { @RequestMapping("/") public String index() { return "hello.html"; } }
- Start the web server application (Run As… > Spring Boot App).
- Open your Web Browser and go to http://localhost:8080/. Now you should see the new content from “hello.html” file.
You can also put static resources inside the directory
src/main/resources/static/
Dynamic content
In order to serve dynamic content you can choose between jsp or thymealef. Spring Boot allows you to use both without any configuration files!
Here there is an example using thymealeaf:
http://spring.io/guides/gs/serving-web-content/
and here an example using jsp:
https://github.com/mariuszs/spring-boot-web-jsp-example
GitHub
You can get the whole code used in this post from our GitHub repository:
https://github.com/netgloo/spring-boot-samples/tree/master/spring-boot-basewebapp
References
http://projects.spring.io/spring-boot/
http://docs.spring.io/spring-boot/docs/current-SNAPSHOT/reference/htmlsingle/#howto-customize-view-resolvers
http://spring.io/guides/gs/spring-boot/
http://spring.io/guides/gs/serving-web-content/
http://kielczewski.eu/2014/04/spring-boot-mvc-application/
spring boot samples
-
Chawqi Hajar
-
Andrea
-
Chawqi Hajar
-
Andrea
-
Chawqi Hajar
-
Andrea
-
Chawqi Hajar
-
-
-
-
-
SwaySwayJoy
-
Andrea
-
-
Mrutyunjaya Tripathy
-
Andrea
-
-
Arun Singh
-
Rakhi Jain
-
Faizan Mubasher