Spring Boot详解(三):Thymeleaf和Spring boot整合

Spring Boot详解(三):Thymeleaf和Spring boot整合

本篇仅仅介绍spring boot和thymeleaf整合的一个小案例。具体thymeleaf的详解参看[Spring Boot详解(二):thymeleaf](Spring Boot详解(二):thymeleaf.md)。

导入依赖

<dependency>
    <groupId>org.springframework.bootgroupId>
    <artifactId>spring-boot-starter-thymeleafartifactId>
    <version>1.5.8.RELEASEversion>
dependency>

页面hello.html(在resources/templates文件夹下)


<html xmlns:th="http://www.thymeleaf.org">
<head>
    <title>Thymeleaf templatetitle>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
head>
<body>

<p>Hello Worldp>

<h1 th:text="${book.bookName}">ssssh1>

body>
html>

Controller层代码 BookController.java

package com.javen.demo_springboot.web;

import com.javen.demo_springboot.bean.Book;
import com.javen.demo_springboot.dao.BookDAO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

@Controller
public class BookController {

    @Autowired
    private BookDAO bookDAO;

    @RequestMapping("/find")
    public String findBook(Map map){
        Book book = bookDAO.queryBookById();
        map.put("book",book);
        System.out.println("-----------------------------" + book.getBookName());
        return "/hello";
    }
}

你可能感兴趣的:(Spring)