DEV Community

Full Stack Hacker
Full Stack Hacker

Posted on

Create a project Spring Boot in Intellij Community Edition

You can install a plugin called Spring Boot Assistant, Spring initializr and assistant:

Now you can use the initializer as:


Declare some project information such as:

  • Project type: is to choose what kind of package manager, Maven or Gradle.
  • Language: Choose the code language, here I choose Java
  • Type of build file: With Spring Boot, you should choose JAR to help configure the Tomcat server
  • Java version: Choose java 8

Choose dependencies (can be understood as auxiliary libraries)

  • Lombok: recommended, it makes Java code shorter, but need to install Lombok plugin in IDE too
  • Thymeleaf: Thymeleaf will help pass data into the view of the MVC model, returning an HTML page with data to the client
  • Spring Web: Build web, including RESTful, applications using Spring MVC. Uses Apache Tomcat as the default embedded container.
  • Spring configuration processor: Generate metadata for developers to offer contextual help and "code completion" when working with custom configuration keys (ex.application.properties/.yml files). Click Create to generate project. When display project, click "Load Maven Project" Click on the green arrow to run the project

Create a simple controller in Spring Boot

Create a "controller" folder to store controller files. Then create the file "DemoController" and write the following code to create the following controller:

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class DemoController {
    @RequestMapping("/hello")
    public String sayHello(@RequestParam(value = "name") String name) {
        return "<h1>Hello " + name + "!</h1>";
    }
}
Enter fullscreen mode Exit fullscreen mode

Run main function:

Spring Boot vs Thymeleaf

Create a simple controller

Thymeleaf + Static files

For Thymeleaf template files, put in src/main/resources/templates/

Create index.html file

<!DOCTYPE HTML>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
    <title>Spring Boot Thymeleaf Hello World Example</title>
    <link rel="stylesheet" th:href="@{css/bootstrap.min.css}"/>
    <link rel="stylesheet" th:href="@{css/main.css}"/>
</head>
<body>
    <nav class="navbar navbar-expand-md navbar-dark bg-dark fixed-top">
        <a class="navbar-brand" href="#">TopTech</a>
    </nav>

    <main role="main" class="container">

        <div class="starter-template">
            <h1>Spring Boot Web Thymeleaf Example</h1>
            <h2>
                <span th:text="'Hello, ' + ${message}"></span>
            </h2>
        </div>

        <ol>
            <li th:each="task : ${tasks}" th:text="${task}"></li>
        </ol>
    </main>
</body>
<script type="text/javascript" th:src="@{js/bootstrap.min.js}"></script>
</html>
Enter fullscreen mode Exit fullscreen mode

Edit application.properties

For Static files like CSS or JS, put in src/main/resources/static/

Download the bootstrap.min.css library and place it in the src/main/resources/static/css directory

Download the bootstrap.min.js library and place it in the src/main/resources/static/js directory

Demo

Run main function:


Source code

https://github.com/java-cake/spring-boot/tree/main/helloworld

Top comments (0)