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