第三章 SpringBoot整合视图层技术

2019-05-05  本文已影响0人  shenyoujian

1、整合thymeleaf(官方推荐)

1.1 thymeleaf是什么

java新一代的模板引擎,支持html原型,可以让前端工程师
直接浏览器查看样式,也可以支持后端工程师结合真实数据查看效果。

1.2 整合步骤
<dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
            <version>1.5.12.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
            <version>1.5.12.RELEASE</version>
        </dependency>
    </dependencies>
1.3 实战
<?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.ljs</groupId>
    <artifactId>thymeleaf</artifactId>
    <version>1.0-SNAPSHOT</version>

    <name>thymeleaf</name>
    <description>Demo project for Spring Boot</description>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.3.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <!--统一项目字符集编码和java版本-->
    <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>
    </dependencies>
</project>
@SpringBootApplication
public class Application {

    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
}
public class Book {

    private Integer id;
    private String name;
    private String author;

//省略get和set
@Controller
public class BookController {

    @GetMapping("/books")
    public ModelAndView Books(){

        List<Book> books = new ArrayList<>();
        Book book1 = new Book();
        book1.setId(1);
        book1.setName("springboot");
        book1.setAuthor("王松");

        Book book2 = new Book();
        book2.setId(2);
        book2.setName("vue");
        book2.setAuthor("王松");

        books.add(book1);
        books.add(book2);

        ModelAndView mv = new ModelAndView();
        mv.addObject("books", books);
        //视图名为books
        mv.setViewName("books");
        return mv;
    }
}
<!DOCTYPE html>
<html lang="en" xmln:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>图书列表</title>
</head>
<body>
<table border="1">
    <tr>
        <td>id</td>
        <td>书名</td>
        <td>作者</td>
    </tr>
    <tr th:each="book:${books}">
        <td th:text="${book.id}"/>
        <td th:text="${book.name}"/>
        <td th:text="${book.author}"/>
    </tr>
</table>

</body>
</html>
1.5 更多关于thymeleaf的基础用法,

thymeleaf官网

2、整合FreeMarker

2.1 freemarker

与Thymeleaf不同 FreeMarker 需要经过解析才能够在浏览器中展示出来。 freeMarker不仅可以用来配置HTML页面模板,也可以作为电子邮件模板、配置文件模板以及源码模板等。

2.2 整合步骤
    <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-freemarker</artifactId>
        </dependency>
3.3 实战
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>图书列表</title>
</head>
<body>
<hr>
<table border="1">
    <tr>
        <td>图书编号</td>
        <td>图书名称</td>
        <td>图书作者</td>
    </tr>
<#if books ??&&(books?size>0)>
    <#list books as book>
        <tr>
            <td>${book.id}</td>
            <td>${book.name}</td>
            <td>${book.author}</td>
        </tr>
    </#list>
</#if>
</table>
</body>
</html>

3 小结

本章介绍了springboot整合视图层技术,如果项目未完全分离可以使用上面两种代替jsp,如果完全分离后端只需提供接口而不需要整合这些。

上一篇下一篇

猜你喜欢

热点阅读