"Spring boot 非常适合 web 应用程序开发。您可以轻松创建自包含的 HTTP 应用。web 服务器采用嵌入式 Tomcat,或者 Jetty 等。大多数情况下 Web 应用程序将使用
spring-bootstarter-web 模块快速启动和运行。"
本例子通过显示用户列表展示如何使用 spring boot 和 Thymeleaf 开发 web 项目。
几点说明:
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来源: https://www.cnblogs.com/junyang/p/8159383.html