美文网首页
Thymeleaf-表达式

Thymeleaf-表达式

作者: 通灵路耳 | 来源:发表于2020-03-07 16:46 被阅读0次
图片.png
1、基于“置顶-SpringBoot-最终整合”进行

2、实体
package com.llhc.model;

public class Product {
    private String name;
    
    public Product(String name) {
        super();
        this.name = name;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
}


3、控制器

package com.llhc.controller;

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

import com.llhc.model.Product;

@Controller
@RequestMapping("/system")
public class TestThymeleafController {
    /**
     * 
     * Thymeleaf+表达式
     * @author 开发者
     * 
     */
    @RequestMapping("thy1")
    public String thy1(Model model){
        String s = "<p style='color:red'>红色文字</p>";
        Product p = new Product("实体");
        
        model.addAttribute("host1",s);
        model.addAttribute("host2",p);
        return "hello";
    }
}

4、在resources下的templates文件夹,创建hello.html

<!DOCTYPE HTML>
<!-- 声明当前文件是Thymeleaf,可以用th开头 -->
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <title>Thymeleaf</title>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    <link rel="stylesheet" type="text/css" media="all" href="../../webapp/static/css/style.css" th:href="@{/static/css/style.css}"/>
    <script type="text/javascript" src="../../webapp/static/js/thymeleaf.js" th:src="@{/static/js/thymeleaf.js}"></script>
    <script>
        testFunction();
    </script>
    <style>
        h2{
            text-decoration: underline;
            font-size:0.9em;
            color:gray;
        }
    </style>    
</head>
<body>

<!-- 表达式-解析文本 -->
<div class="showing">
    <h2>解析文本</h2>
    <p th:utext="${host1}" ></p>
</div>
<!-- 表达式-解析对象 -->
<div class="showing">
    <h2>解析对象</h2>
    <p th:text="${host2.name}" ></p>
</div>
</body>
</html>

5、访问:http://localhost:8080/system/thy1

相关文章

网友评论

      本文标题:Thymeleaf-表达式

      本文链接:https://www.haomeiwen.com/subject/xtncdhtx.html