三、Spring Boot 之 Web 开发

2017-12-17  本文已影响0人  cqzhangjian

Web 开发的核心内容主要包括 内嵌 Servlet 容器和 Spring MVC。

1. Spring Boot 的 Web 开发的支持

Spring Boot 提供了 spring-web-starter-web 为 web 开发支持,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 包有问题的。不能访问 JSP

Thymeleaf 是一个 Java 类库,它是一个 xml/xhtml/html5 的模板引擎,可以作为 MVC 的 Web 应用的 View 层。它还提供了额外的模块与 Spring MVC 集成,所以可以使用 Thymeleaf 替代 JSP

2.1 编写 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">访问模型中数据</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 编写 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 编写 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){
        //准备数据
        Person p = new Person();
        p.setName("张剑");
        //放入模型中
        model.addAttribute("person", p);
        return "index";
    }
}

2.5 测试结果

图片.png

3. Web 相关配置

通过查看 WebMvcAutoConfiguration 类 、WebMvcProperties 类的源码,发现 Spring Boot 提供如下的自动配置(自动配置 ViewResolver、自动配置静态资源、自动配置的 Formatter 和 Converter、自动配置 HttpMessageConverters 等),在此就不一一介绍自动配置,感兴趣的同学可以去看下源码

上一篇下一篇

猜你喜欢

热点阅读