如果开发者希望开发Spring Boot的Web应用程序,可以在Spring Boot项目的pom.xml文件中,添加如下依赖配置:
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>
     Spring Boot将自动关联Web开发的相关依赖,如tomcat、spring-webmvc等,进而对Web开发的支持,并将相关技术的配置实现自动配置。
Thymeleaf模板引擎

    在Spring Boot的Web应用中,建议开发者使用HTML完成动态页面。Spring Boot提供了许多模板引擎,主要包括FreeMarker、Groovy、Thymeleaf、Velocity和Mustache。因为Thymeleaf提供了完美的Spring MVC支持,所以在Spring Boot的Web应用中推荐使用Thymeleaf作为模板引擎。
    Thymeleaf是一个Java类库,是一个xml/xhtml/html5的模板引擎,能够处理HTML、XML、JavaScript以及CSS,可以作为MVC Web应用的View层显示数据。
Spring Boot的Thymeleaf支持

在Spring Boot 1.X版本中,spring-boot-starter-thymeleaf依赖包含了spring-boot-starter-web模块。但是,在Spring 5中,WebFlux的出现对于Web应用的解决方案将不再唯一。所以,spring-boot-starter-thymeleaf依赖不再包含spring-boot-starter-web模块,需要开发人员自己选择spring-boot-starter-web模块依赖。
Tymeleaf模板默认将JS脚本、CSS样式、图片等静态文件默认放置在src/main/resources/static目录下;将视图页面放在src/main/resources/templates目录下。
<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.dadid</groupId>
    <artifactId>9-20-SpringBootWeb</artifactId>
    <version>0.0.1-SNAPSHOT</version>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.0.RELEASE</version>
        <relativePath /> <!-- lookup parent from repository -->
    </parent>
    
    <properties>
        <!-- 声明项目配置依赖编码格式为 utf-8 -->
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <fastjson.version>1.2.24</fastjson.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>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>
server.servlet.context-path=/ch5_1
#spring.thymeleaf.prefix=classpath:/templates
spring.messages.basename=i18n/admin/adminMessages,i18n/before/beforeMessages,i18n/common/commonMessages
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
测试Spring Boot的Thymeleaf支持<br>
<h1>引入主体内容模板片段:</h1>
<h1>引入版权所有模板片段:</h1>
</body>
</html>
package com.ch.ch5_1.controller;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
//import org.springframework.web.bind.annotation.RestController;

@Controller
//@RestController
public class TestThymeleafController {
    @RequestMapping("/")
    public String test() {
        // 根据Tymeleaf模板,默认将返回src/main/resources/templates/index.html
        return "index";
    }

    @RequestMapping("/testObject")
    public String testObject(Model model) {
        // 系统时间new Date()
        model.addAttribute("nowTime", new Date());
        // 系统日历对象
        model.addAttribute("nowCalendar", Calendar.getInstance());
        // 创建BigDecimal对象
        BigDecimal money = new BigDecimal(2019.613);
        model.addAttribute("myMoney", money);
        // 字符串
        String tsts = "Test strings";
        model.addAttribute("str", tsts);
        // boolean类型
        boolean b = false;
        model.addAttribute("bool", b);
        // 数组(这里不能使用int定义数组)
        Integer aint[] = { 1, 2, 3, 4, 5 };
        model.addAttribute("mya", aint);
        // List列表1
        List<String> nameList1 = new ArrayList<String>();
        nameList1.add("陈恒1");
        nameList1.add("陈恒3");
        nameList1.add("陈恒2");
        model.addAttribute("myList1", nameList1);
        // Set集合
        Set<String> st = new HashSet<String>();
        st.add("set1");
        st.add("set2");
        model.addAttribute("mySet", st);
        // Map集合
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("key1", "value1");
        map.put("key2", "value2");
        model.addAttribute("myMap", map);
        // List列表2
        List<String> nameList2 = new ArrayList<String>();
        nameList2.add("陈恒6");
        nameList2.add("陈恒5");
        nameList2.add("陈恒4");
        model.addAttribute("myList2", nameList2);
        // 根据Tymeleaf模板,默认将返回src/main/resources/templates/showObject.html
        return "showObject";
    }
}
package com.ch.ch5_1.controller;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class APP {
    public static void main(String[] args) {
        SpringApplication.run(APP.class);
    }
}

吴裕雄--天生自然--Spring Boot的Web开发支持_web应用

 

吴裕雄--天生自然--Spring Boot的Web开发支持_xml_02