In this post, i am going to demo spring boot using tomcat (embed) and thymeleaf template engine.Tools:
- spring tool suite (STS): Version: 3.9.1.RELEASE
- maven 3.5
- spring boot version 1.5.8
- Thymeleaf templating engine, including integration with Spring.
Step1: create new spring starter project
Structure of project after create.
Pom.xml (auto generate for us)
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.shine2share</groupId>
<artifactId>springboot_demo1</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>springboot_demo1</name>
<description>Building project by spring boot</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.8.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
SpringbootDemo1Application.java (run this class to start spring boot application)
package com.shine2share;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class SpringbootDemo1Application {
public static void main(String[] args) {
SpringApplication.run(SpringbootDemo1Application.class, args);
}
}
Step2: create a simple controller and simple html page (index.html) under src/main/resources -> templates:
HomeController.java
package com.shine2share.controller;import org.springframework.stereotype.Controller;import org.springframework.ui.Model;import org.springframework.web.bind.annotation.RequestMapping;@Controller public class HomeController { @RequestMapping("/")model.addAttribute("name", "Spring Developer.");public String sayHello(Model model) { return "index"; }}
index.html:
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org"
xmlns:sec="http://www.thymeleaf.org/thymeleaf-extras-springsecurity4">
<head>
<meta charset="UTF-8" />
<title>Home</title>
</head>
<body>
<h1><span th:text="'Hello: ' + ${name}"></span></h1>
</body>
</html>
ok.
check result with following url:
if your application don't show like above.
you can check your spring boot is running: (click to image to show full size)
if it stopped, you can run it again, right mouse click on project -> run as -> spring boot app
check url again.
happy coding.
0 nhận xét:
Đăng nhận xét