如何在百里香叶中传递地图(春季4)

时间:2017-03-08 09:57:50

标签: spring-mvc spring-boot thymeleaf

我想使用百万美元模板引擎从属性文件传递一个地图。

例外:

org.springframework.expression.spel.SpelEvaluationException: EL1008E: Property or field 'title' cannot be found on object of type 'java.lang.String' - maybe not public?

provider.html:

<!DOCTYPE>
<html th:include="receiver :: receiver(#{site})"></html>

receiver.html:

<!DOCTYPE HTML>
<html th:fragment="receiver(map)">
    <head>
        <title th:text="${map.title}">title</title>
    </head>
    <body th:text="${map.body}">
        body
    </body>
</html>

messages.properties:

site.title = Any title
site.body = Any body

控制器:

@Controller
public class StartController {

    @RequestMapping(value = "/", method = RequestMethod.GET)
    public String start(Model model) {
        return "provider";
    }

}

2 个答案:

答案 0 :(得分:0)

它就像在Java中一样。地图的关键和价值

<div th:each="userEnrty: ${userMap}">
<p th:text="${userEntry.key}">No Id!</p>
<p th:text="${userEntry.value}">No Name!</p>
</div>

答案 1 :(得分:0)

所以我发现属性文件被处理为Map。所以'网站'根本不是地图。

我现在的解决方案是传递变量prefi的名称,并通过百里香预处理来获取密钥。

provider.html:

<!DOCTYPE>
<html th:include="receiver :: receiver('site')"></html>

receiver.html:

<!DOCTYPE HTML>
<html th:fragment="receiver(mapname)">
    <head>
        <title th:text="#{__${mapname}__.title}">title</title>
    </head>
    <body th:text="#{__${mapname}__.body}">
        body
    </body>
</html>

messages.properties

site.title = Any title
site.body = Any body