“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.yuny</groupId> <artifactId>myweb</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>Spring-boot-web</name> <description>web project for Spring Boot</description> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.9.RELEASE</version> <relativePath/> </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>
增加實體User
public class User { private Integer id; private String name; private String age; private String address; //省略get和set方法、構造函數 }
增加UserController
@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 xmlns:th="http://www.thymeleaf.org"> <head> <meta charset="UTF-8" /> <title>Insert title here</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>
啟動
以application方式啟動SpringBootWebApplication.java
訪問http://localhost:8080/user/list ,效果如下
總結
Spring boot開發web項目非常簡單,對模版的支持也很到位。Thymeleaf模版引擎跟el表達式很相似,所以從jsp過度到使用Thymeleaf 並不是太難的事。
本文案例代碼下載地址
https://github.com/junyanghuang/spring-boot-samples/tree/master/Spring-boot-web