其他分享
首页 > 其他分享> > SpringBoot+thymelates入门

SpringBoot+thymelates入门

作者:互联网

在pom.xml当中加入这俩个依赖

         <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>
        <dependency>
            <groupId>net.sourceforge.nekohtml</groupId>
            <artifactId>nekohtml</artifactId>
            <version>1.9.22</version>
        </dependency>

NekoHTML 是HTML扫描器和标签补偿器,能增补缺失的父元素、自动用结束标签关闭相应的元素,以及不匹配的内嵌元素标签。

application.properties

##端口号
server.port=8888
##去除thymeleaf的html严格校验
spring.thymeleaf.mode=LEGACYHTML5
#设定thymeleaf文件路径 默认为src/main/resources/templates
spring.freemarker.template-loader-path=classpath:/templates
#设定静态文件路径,js,css等
spring.mvc.static-path-pattern=/static/**
#是否开启模板缓存,默认true
#建议在开发时关闭缓存,不然没法看到实时页面
spring.thymeleaf.cache=false
#模板编码
spring.freemarker.charset=UTF-8

ThymeleafController.java

@Controller
public class ThymeleafController {
    @RequestMapping(value = "/")
    public String thymeleafTest(ModelMap modelMap) {
        modelMap.addAttribute("msg", "你好公子潘");
        modelMap.addAttribute("tag", "http://localhost:8080/gtvg/order/details");
        int[] s=new int[]{9,9,34};
        modelMap.addAttribute("one","s");
        List<Map<String,Object>>list=new ArrayList<>();
        Map<String,Object>m=new HashMap<>();
        m.put("name","pan");
        list.add(m);
        m=new HashMap<>();
        m.put("name","chen");
        list.add(m);
        m=new HashMap<>();
        m.put("name","chenyiqiao");
        list.add(m);
        m=new HashMap<>();
        m.put("name","kai");
        list.add(m);

        modelMap.addAttribute("list",list);
        return "thymeleaf";
    }

    @RequestMapping(value = "/tag")
    public String tag()
    {
        return "test";
    }
}

thymeleaf.html

<!DOCTYPE html>
<html lang="en">
<html lang="en" xmlns:th="http://www.w3.org/1999/xhtml">

<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<div th:text="${msg}"></div>
<div th:text="${tag}"></div>
<input type="text" th:value="${msg}">
<a th:href="@{https://cdn2.jianshu.io/assets/web/nav-logo-4c7bbafe27adc892f3046e6978459bac.png}">
    超链接</a>
<a th:href="@{tag}">controller跳转</a>
<img th:src="${'https://123p3.sogoucdn.com/imgu/2018/04/20180404144123_595.png'}"/>

<div th:each="m,iterStat : ${list}">
    <div th:text="${m.name}"></div>
    <span th:text="'index:'+${iterStat.index}"></span>
    <span th:text="${'count:'+iterStat.count}"></span>
    <span th:text="'current:'+${iterStat.current}"></span>
    <span th:text="${'odd:'+iterStat.odd}"></span>
    <span th:text="${'even:'+iterStat.even}"></span>
    <span th:text="${'size:'+iterStat.size}"></span>
</div>
</body>
</html>

test.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
test
</body>
</html>

个人网站

标签:thymelates,SpringBoot,spring,modelMap,list,thymeleaf,new,HashMap,入门
来源: https://www.cnblogs.com/panbingwen/p/10703252.html