thymeleaf标签获取后台数据

不多bb,代码如下:
后台

	model.addAttribute("a",a);//a是对象
	model.addAttribute("b", b);
	model.addAttribute("c", b);
	model.addAttribute("d", d);
	model.addAttribute("e", e);
	return new ModelAndView("usercenter/usercenter",model);

前端

<p th:html="'+${a.userName}+'"></p><!-- 如果下面这个不行就用这个 -->
<p th:text="'+${a.age}+'"></p><!-- 有些情况使用th:html也是不显示,还是根据情况选择使用吧 -->
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要让所有Thymeleaf页面获取后台的公用数据,可以使用Thymeleaf的Context对象。 首先,在Controller中,将公用数据放入Context对象中,然后将Context对象添加到Model中: ``` @Controller public class MyController { @ModelAttribute public void addCommonDataToModel(Model model) { Context context = new Context(); context.setVariable("commonData", "公用数据"); model.addAttribute("context", context); } } ``` 在Thymeleaf页面中,可以使用th:each和th:object来遍历Context对象中的所有属性: ``` <!DOCTYPE html> <html xmlns:th="http://www.thymeleaf.org"> <head> <title>Thymeleaf Page</title> </head> <body> <h1>Thymeleaf Page</h1> <div th:each="entry : ${context.expressionObjects}"> <p> <span th:text="${entry.key}"></span>: <span th:text="${entry.value}"></span> </p> </div> </body> </html> ``` 这样,在所有Thymeleaf页面中,都可以使用${context.commonData}来获取公用数据了。 另外,如果需要在所有Thymeleaf页面中使用相同的布局或模板,可以使用Thymeleaf的布局功能。首先,定义一个布局模板: ``` <!DOCTYPE html> <html xmlns:th="http://www.thymeleaf.org"> <head> <title th:text="${title}">Default Title</title> </head> <body> <div th:replace="fragments/header :: header"></div> <div th:replace="${content}"></div> <div th:replace="fragments/footer :: footer"></div> </body> </html> ``` 其中,fragments/header和fragments/footer是两个片段模板。在具体的页面中,可以使用Thymeleaf的片段替换功能来替换content属性: ``` <!DOCTYPE html> <html xmlns:th="http://www.thymeleaf.org"> <head> <title>Thymeleaf Page</title> </head> <body> <div th:replace="fragments/layout :: content"> <div th:replace="fragments/my-content :: myContent"></div> </div> </body> </html> ``` 这样,在所有Thymeleaf页面中,都可以使用相同的布局和模板了。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值