栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Thymeleaf入门

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Thymeleaf入门

一、创建一个Maven项目

 如下创建了名为ctest_thymeleaf项目

二、导入坐标

 在pox.xml加入thymeleaf坐标


        org.springframework.boot
        spring-boot-starter-parent
        2.6.5
        
    


    

        
            org.springframework.boot
            spring-boot-starter-web
        

        
            org.springframework.boot
            spring-boot-starter-thymeleaf
        

 一定要如下图标刷新

三、测试

 最后项目结构图:

 在resources下创建一个templates目录,在该目录下创建hello.html.
 th:text=“key” 会将从控制器相应的key对应的value放置在该标签文本内
 thymeleaf和html不同的是多了一行xmlns:th="http://www.thymeleaf.org
 在hello.html加入如下代码:




    
    Title


    

 控制器类HelloController:

package com.demo.controller;


import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;


@Controller
public class HelloController {


    @RequestMapping("/h")
    public String he(Model model){
        model.addAttribute("sayHello","Hello Thymeleaf!");
        return "hello";  //返回 指定哪一个模板, 这个是上述的hello.html
    }


}

 SpringBoot启动类DemoApplication:

package com.demo;


import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class DemoApplication {

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

}

 运行DemoApplication

 在浏览器输入http://localhost:8080/h

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/780846.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号