“Spring boot非常適合Web應用程序開發。您可以輕鬆創建自包含的HTTP應用。web服務器採用嵌入式Tomcat,或者Jetty等。大多數情況下Web應用程序將使用spring-bootstarter-web模塊快速啓動和運行。”
本例子通過顯示用戶列表展示如何使用spring boot和Thymeleaf開發web項目。
幾點說明:
- Spring boot開發web項目,通常打成jar包,使用內置的web服務器 Tomcat、Jetty、undertow 來運行。
- 靜態資源(css、js、圖片等)默認放在resources/static下面。如果要修改默認存放目錄,可以通過設置屬性 spring.mvc.static-path-pattern來實現。
- 模板文件默認放在 templates目錄下
- Spring boot支持使用模板來開發web應用,支持的模板類型包括
- FreeMarker
- Groovy
- Thymeleaf
- Mustache
Spring boot不建議使用jsp開發web。
本文使用Thymeleaf來作爲模板引擎開發web項目。
Thymeleaf
Thymeleaf是一個Java模板引擎開發庫,可以處理和生成HTML、XML、JavaScript、CSS和文本,在Web和非Web環境下都可以正常工作。
Thymeleaf可以跟Spring boot很好的集成。
Spring Boot+Thymeleaf開發web
創建spring boot項目
選擇spring boot和依賴 ,注意需要的依賴包括web和Thymeleaf
點擊finish。創建的項目結構如下:
其中SpringBootWebApplication.java是自動生成的。是程序啓動入口。
生成的POM.xml文件如下:
<?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.sample</groupId>
<artifactId>web</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>spring-boot-web</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.13.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>
在com.example.demo.model中增加實體User
package com.example.demo.model;
public class User {
private Integer id;
private String name;
private Integer age;
private String address;
public User() {
}
public User(Integer id, String name, Integer age, String address) {
super();
this.id = id;
this.name = name;
this.age = age;
this.address = address;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
@Override
public String toString() {
return "User [id=" + id + ", name=" + name + ", age=" + age + ", address=" + address + "]";
}
}
在com.example.demo.controller中增加UserController
package com.example.demo.controller;
import java.util.ArrayList;
import java.util.List;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import com.example.demo.model.User;
@Controller
@RequestMapping("/user")
public class UserController {
@RequestMapping("/{id}")
public String getUser(@PathVariable Integer id,Model model) {
model.addAttribute("user",new User(id,"張三",20,"中國廣州"));
return "/user/detail";
}
@RequestMapping("/list")
public String listUser(Model model) {
List<User> userList = new ArrayList<User>();
for (int i = 0; i <10; i++) {
userList.add(new User(i,"張三"+i,20+i,"中國廣州"));
}
model.addAttribute("users", userList);
return "/user/list";
}
}
增加模版文件list.html,注意模版文件是放在tempplates目錄下。本案例將文件放在/templates/user/下面。
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>User List</title>
</head>
<body>
<h2>用戶列表</h2>
<div>
<ul>
<li th:each="user:${users}">
<span th:text="${user.id}"></span>-
<span th:text="${user.name}"></span>-
<span th:text="${user.age}"></span>-
<span th:text="${user.address}"></span>
</li>
</ul>
</div>
</body>
</html>
啓動
以Spring Boot App方式啓動SpringBootWebApplication.java
訪問http://localhost:8080/user/list ,效果如下
總結
Spring boot開發web項目非常簡單,對模版的支持也很到位。Thymeleaf模版引擎跟el表達式很相似,所以從jsp過渡到使用Thymeleaf 並不是太難的事。
本文案例代碼下載地址