thymeleaf获取后台传到页面上的html标签

1、后台代码
@RequestMapping("/enterPriseBusiness/add")
    public String addEntBusinessInfo(Model model) {
        model.addAttribute("title", "企业中文名:     ");
        model.addAttribute("tclass","col-xs-2 required");
        model.addAttribute("tstyle","text-align:right;background:blue;font:italic bold 12px/20px arial,sans-serif;")
        return PREFIX + "business/enterPriseBusinessAdd";
       }

2、thymeleaf取值
<label for="enterpriseCN" th:class="${tclass}" th:style="${tstyle}" th:utext="${title}"></label>


要让所有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页面中,都可以使用相同的布局和模板了。
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值