Create a project Spring Boot in Intellij Community Edition
Tags: spring, springboot
Published: 2022-07-19 04:18:19

You can install a plugin called **Spring Boot Assistant, Spring initializr and assistant**:
![](https://i.imgur.com/4mnLz0t.png)
Now you can use the initializer as:
![](https://i.imgur.com/OsBIyR8.png)
![](https://i.imgur.com/8TIRYhO.png)
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
![](https://i.imgur.com/xCAZ5lS.png)

#### 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).
![](https://i.imgur.com/8mbWHtP.png)
![](https://i.imgur.com/nzIc59R.png)
Click **Create** to generate project. When display project, click "Load Maven Project"
![](https://i.imgur.com/PDv5lUz.png)
Click on the green arrow to run the project
![](https://i.imgur.com/D50eLIJ.png)
![](https://i.imgur.com/Damq2r1.png)

### 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:
![](https://i.imgur.com/znESVR4.png)
```java
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>";
    }
}
```
Run main function:
![](https://i.imgur.com/D50eLIJ.png)
![](https://i.imgur.com/Z7MtIMY.png)

### Spring Boot vs Thymeleaf
#### Create a simple controller
![](https://i.imgur.com/KcvRcxR.png)

#### Thymeleaf + Static files

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

Create **index.html** file

```html
<!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>
```

Edit **application.properties**

![](https://i.imgur.com/eElMjzv.png)

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

![](https://i.imgur.com/rHjYLOt.png)

Download the [bootstrap.min.css](https://cdn.jsdelivr.net/npm/bootstrap@4.3.1/dist/css/bootstrap.min.css) library and place it in the **src/main/resources/static/css** directory

Download the [bootstrap.min.js](https://cdn.jsdelivr.net/npm/bootstrap@4.3.1/dist/js/bootstrap.min.js) library and place it in the **src/main/resources/static/js** directory

#### Demo

Run main function:

![](https://i.imgur.com/D50eLIJ.png)
![](https://i.imgur.com/GdeiIFf.png)
#### Source code
https://github.com/java-cake/spring-boot/tree/main/helloworld