三、Spring Boot 之 Web 開(kāi)發(fā)

Web 開(kāi)發(fā)的核心內(nèi)容主要包括 內(nèi)嵌 Servlet 容器和 Spring MVC。

1. Spring Boot 的 Web 開(kāi)發(fā)的支持

Spring Boot 提供了 spring-web-starter-web 為 web 開(kāi)發(fā)支持,Spring-boot-starter-web 提供了 嵌入的 Tomcat 以及 SpringMVC 的依賴

<?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>org.zj.study.springboot.web</groupId>
    <artifactId>springboot-web-demo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>springboot-web-demo</name>
    <description>Demo project for Spring Boot</description>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.9.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-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>

2. Thymeleaf 模板引擎 (官方推薦使用的模板)

注意:使用 Spring Boot 選 JSP 作模板,打成 jar 包有問(wèn)題的。不能訪問(wèn) JSP

Thymeleaf 是一個(gè) Java 類庫(kù),它是一個(gè) xml/xhtml/html5 的模板引擎,可以作為 MVC 的 Web 應(yīng)用的 View 層。它還提供了額外的模塊與 Spring MVC 集成,所以可以使用 Thymeleaf 替代 JSP

2.1 編寫(xiě) Thymeleaf 模板 index.html

<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8"></meta>
<title>Thymeleaf</title>
<!-- bootstrap 樣式引入 -->
<link th:src="@{bootstrap/css/bootstrap.css}" />
<link th:src="@{bootstrap/css/bootstrap-theme.css}" />
<!-- jquery、bootstrap js 引入 -->
<script type="text/javascript" th:src="@{jquery/jquery.js}"></script>
<script type="text/javascript" th:src="@{bootstrap/js/bootstrap.js}"></script>
</head>
<body>
    <div class="panel panel-primary">
        <div class="panel-heading">
            <h3 class="panel-title">訪問(wèn)模型中數(shù)據(jù)</h3>
        </div>
        <div class="panel-body">
            <span th:text="${preson.name}"></span>
        </div>
    </div>
</body>
</html>

2.2 Spring Boot 添加 Thymeleaf 支持

pom 文件添加如下依賴

    <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>

2.3 編寫(xiě) Person 模型類

package org.zj.study.springboot.web.springbootwebdemo.model;

import java.util.Date;

public class Person {

    private String name;
    private Date birth;
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public Date getBirth() {
        return birth;
    }
    public void setBirth(Date birth) {
        this.birth = birth;
    }
}

2.4 編寫(xiě) Controller 類 PersonController

package org.zj.study.springboot.web.springbootwebdemo.controller;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.zj.study.springboot.web.springbootwebdemo.model.Person;

@Controller
public class PersonController {

    @RequestMapping("/")
    public String index(Model model){
        //準(zhǔn)備數(shù)據(jù)
        Person p = new Person();
        p.setName("張劍");
        //放入模型中
        model.addAttribute("person", p);
        return "index";
    }
}

2.5 測(cè)試結(jié)果

圖片.png

3. Web 相關(guān)配置

通過(guò)查看 WebMvcAutoConfiguration 類 、WebMvcProperties 類的源碼,發(fā)現(xiàn) Spring Boot 提供如下的自動(dòng)配置(自動(dòng)配置 ViewResolver、自動(dòng)配置靜態(tài)資源、自動(dòng)配置的 Formatter 和 Converter、自動(dòng)配置 HttpMessageConverters 等),在此就不一一介紹自動(dòng)配置,感興趣的同學(xué)可以去看下源碼

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容