如何在HTML页面中解析thyemeleaf上下文变量

时间:2017-10-16 08:06:30

标签: spring-boot thymeleaf

面对Thymeleaf模板引擎的问题。 我需要在html页面中添加一些动态内容。

我有这样的地图

Map<String, String> info = new HashedMap<String, String>();
info.put("user_name", "username");
info.put("host_user_name", "host_user_name");
info.put("title", "Element_title");
info.put("url", "https://www.google.com");
info.put("conv_url","/#/home/" + "23243242" + "/services?fid=543545"&fview=conversation")

我正在将地图中的信息添加到上下文

MimeMessage message = sender.createMimeMessage();
MimeMessageHelper helper = new MimeMessageHelper(message, true);
Context context = new Context();
System.out.println("---------0------------");
info.forEach((key, value) -> {
    context.setVariable(key, value);
});
String template = templateEngine.process("my-html", context);

my-html就像这样

<table width="115" border="0" cellspacing="0" cellpadding="0" >
    <tr>
        <td align="right" valign="top" class="mob_center" style="padding:15px">
            <a target="_blank" style="color: #596167; font-family: Arial, Helvetica, sans-serif; font-size: 13px;">
            <font face="Arial, Helvetica, sans-seri; font-size: 13px;" size="3" color="#596167">
            <img src="${url}/static/images/logo.png" width="150" height="auto" alt="Logo" border="0" style="display: block;" /></font></a>
        </td>
    </tr>
</table>
<table style="width:100%">
<tr>
    <td style="font-size: 12px; padding-top:5px;    color: #000;    line-height: 16px;    text-align: left;" th:inline="text">
        <span style="font-weight:600; display:block; " th:title="
        ${host_user_name}">[[${host_user_name}]] </span>[[${title}]]
    </td>
</tr>

<tr>
    <td width="40" valign="top">&nbsp;</td>
    <td>
        <table style="width:100%">
        <tr>
            <td style="font-size: 13px;    color: #fff;    line-height: 16px;    text-align: left;" th:inline="text">
                <a title="" style="color:#000; text-decoration:none; font-weight:500; font-size:12px;">[[${followers}]] followers</a> | <a title="" style="color:#000; text-decoration:none; font-weight:500; font-size:12px;" th:inline="text">[[${replies}]] replies</a>
            </td>
        </tr>
        <tr>
            <td  colspan="2" style="text-align:center">
                <a href="${url}${conv_url}"><img src="${url}/static/images/Join.png" title="" alt="Join the Conversation" /></a>
                  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
            </td>
        </tr>
        </table>

内联文本正在为我修改,但是如何更改href和src属性值。

<td colspan="2" style="text-align:center">
    <a href="${url}${conv_url}"><img src="${url}/static/images/Join.png" title="" alt="Join the Conversation" /></a>
</td>

这应该替换为

<td  colspan="2" style="text-align:center">
    <a href=https://www.googl.com/#/home/23243242/services?fid=543545&fview=conversation"><img src="https://www.googl.com/static/images/Join.png" title="" alt="Join the Conversation" /></a>
</td>

有人可以帮我吗?

2 个答案:

答案 0 :(得分:2)

请阅读documentation

特别是,请查看本文档中的此示例:

<a th:href="@{/order/details(id=${order.id},action=(${user.admin} ? 'show_all' : 'show_public'))}">

您必须使用th:href

答案 1 :(得分:0)

这对我有用

  $scope.pasteFunction = function(ev) {
    $scope.pasted = ev.clipboardData.getData('text').split(" ").map(Number);
    console.log($scope.pasted);
  }