thymeleaf 的使用(一)--導入和基本操作


首先導入thymeleaf:

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

使用:

只要把HTML頁面放在classpath:/templates/下, thymeleaf就能自動渲染

例如, 在resources目錄下:

  resources目錄下創建hello.html

  templates目錄下創建success.html

controller代碼如下:

package com.ryan.springdemothymeleaf.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

@Controller
public class Hellocontroller {

    @ResponseBody
    @RequestMapping("/Hello")
    public String hello(){
        return "Hello World!";
    }

    @RequestMapping("/success")
    public String success(){
        return "success.html";
    }
}

在瀏覽器訪問 localhost:8080/hello.html 可訪問hello.html文件

在瀏覽器訪問 localhost:8080/success 可訪問success.html文件 (不添加.html)


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM