springboot中thymeleaf模板引擎获取数据以及配置---9

配置

在application.properties文件中增加Thymeleaf模板的配置。

#thymelea模板配置
spring.thymeleaf.prefix=classpath:/templates/
spring.thymeleaf.suffix=.html
spring.thymeleaf.mode=HTML5
spring.thymeleaf.encoding=UTF-8
spring.thymeleaf.content-type=text/html
spring.thymeleaf.cache=false
spring.resources.chain.strategy.content.enabled=true
spring.resources.chain.strategy.content.paths=/**

更多参考https://blog.csdn.net/ncc1995/article/details/100019867

获取数据

简单表达式

   ${...}  变量表达式

  *{...}  选择变量表达式

  #{...}  消息表达式

  @{...}  链接url表达式

在这里插入图片描述
1.一般变量的取值:

<h1 th:text="${name}"></h1>

2.从对象中取值:

<input type="text" name="name" value="James Carrot" th:value="${user.name}" />

3,list循环迭代

<ul>
 <li th:each="item: ${list}" th:text="${item}"></li>
</ul>

更多参考
https://blog.csdn.net/qq_22860341/article/details/79229181

前端从对象中获得属性实例

controller中

@Controller
public class TestString {
//    这是测试controller 与restController返回的是视图名还是字符串
    @RequestMapping("/test6")
    public String getcontroller(Model model) {
        Person person = new Person("aa",11);
        List<Person> people = new ArrayList<Person>();
        Person p1 = new Person("xxx",12);
        Person p2 = new Person("yyy",14);
        Person p3 = new Person("zzz",13);
        people.add(p1);
        people.add(p2);
        people.add(p3);
        model.addAttribute("singleperson",person);
        model.addAttribute("people",people);
        return "test";
    }
}

HTML中

<!DOCTYPE html>
<html lang="en"
      xmlns="http://www.w3.org/1999/xhtml"
      xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>测试</title>
</head>
<body>
<div class="panel-body">
    <span th:text="${singleperson.name}"></span>
</div>
<ul class="list-group">
    <li class="list-group-item" th:each="person:${people}">
        <span th:text="${person.name}"></span>
        <span th:text="${person.age}"></span>
        <button class="btn" th:οnclick="'getName(\'' + ${person.name} + '\');'">获得名字</button>
    </li>
</ul>

</body>
</html>

显示效果

前端从List中获取数据

看上面的代码
html代码参考

<ul class="list-group">
 <li class="list-group-item" th:each="person:${people}">
<span th:text="${person.name}"></span>
<span th:text="${person.age}"></span>
<button class="btn" th:οnclick="'getName(\'' + ${person.name} + '\');'">获得名字</button>
</li>
</ul>

前端从Map中获取数据

controller中

@Controller
public class TestString {
//    这是测试controller 与restController返回的是视图名还是字符串
    @RequestMapping("/test6")
    public String getcontroller(Model model) {
        Person person = new Person("aa",11);
        List<Person> people = new ArrayList<Person>();
        Person p1 = new Person("xxx",12);
        Person p2 = new Person("yyy",14);
        Person p3 = new Person("zzz",13);
        people.add(p1);
        people.add(p2);
        people.add(p3);
        Map<String,Person> map = new HashMap<String, Person>();
        map.put("aa",new Person("bb",11));
        map.put("bbb",new Person("BBBB",13));
        map.put("ccc",new Person("DDD",14));
        model.addAttribute("map1",map);
        model.addAttribute("singleperson",person);
        model.addAttribute("people",people);
        return "test";
    }
}

html中

<table border="1" >

    <tr th:each="userEntry,userStat:${map1}">
        <td th:text="${userStat.index}"></td>
        <td th:text="${userStat}"></td>
        <td th:text="${userEntry}"></td>
        <td th:text="${userEntry.value}"></td>
        <td th:text="${userEntry.value.name}"></td>
        <td th:text="${userEntry.value.age}"></td>
    </tr>
</table>

效果展示

参考

SpringBoot整合thymeleaf
Spring Boot框架入门教程(快速学习版)

  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

RDSunday

爱,就供养;喜欢/受益,就打赏

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值