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

  1. Open Eclipse STS.
  2. Right click on the project panel (or go on Menu > File) then New > Spring Starter Project.
  3. 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.

  4. Click Next. Search and select the “Web” dependency, then click Finish.
  5. 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

  1. 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!!!";
      }
    
    }
    
  2. Right click on the project folder > Run As… > Spring Boot App
  3. 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

  1. 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>
  2. Remove the @ResponseBody annotation and specify the file that will be returned (“hello.html”) on the controller class BasicController:
    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";
      }
    
    }
  3. Start the web server application (Run As… > Spring Boot App).
  4. 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

    hello ! i want first to thank you for your tutoriels , they are very clear . actually i am working on spring boot project with bootstrap template that i add to the folder template and i want to display one of the pages of my template with the controller .it gives me a blanck page so i return to this tutoriel but unfortuently i had a white label page .Can you help please?

    • Hello!.. I don’t understand very well your problem… Are you trying to serving static html pages? If so you should put them inside the folder src/main/reources/public as described in this post, then put the right path in your controller method..

      Instead if you are using thymeleaf you can try to follow this great tutorial: http://spring.io/guides/gs/serving-web-content/

      • Chawqi Hajar

        No i am using a bootstrap template i have all my pages in the folder with the path templates/pages

        • But, is it a boostrap template of static hmtl pages? Or are you using some template engine (e.g. thymeleaf) or jsp pages?
          What is the path from the project’s root folder? Is it src/main/resources/public/template/pages?

          • Chawqi Hajar

            Yes ,here’s the structure of my project . I am using angularjs in client side but i don’t know how to use it to dislay views, i thought that it’s my java controller who will display the view but it’s not the case

          • You can try to put all the content from the folder startbootstrap-sb-admin-2-1.0.7 to src/main/resources/static (or also public instead of static), then return “pages/hello.html” from the controller…

            But if you are using Spring Boot + Angular JS I reccomend you to take a look to JHipster that is a great project generator: http://jhipster.github.io/

          • Chawqi Hajar

            Okay thank you 🙂

  • SwaySwayJoy

    I followed the tutorial when serving an html page but it doesn’t serve the html page it just prints the string. Might there be something I’m missing?

    • Maybe do you forget to remove the @ResponseBody annotation from above the controller?

      You can also try to download the code from our GitHub repository here and run it. It is already configured for serving an html page. Then you can try to compare it with your code.

Categories

Category BootstrapCategory CoffeescriptCategory DrupalCategory GravCategory HTMLCategory JavascriptCategory JoomlaCategory jQueryCategory LaravelCategory MagentoCategory PHPCategory SharePointCategory SpringCategory ThymeleafCategory WordPressCategory Workflow

Comments

Developed and designed by Netgloo
© 2017 Netgloo