2017-03-08 87 views
0

我想使用百里香叶模板引擎从属性文件中传递地图。如何通过百里香叶中的地图(春季4)

例外:

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"; 
    } 

} 

回答

0

它只是在Java中一样。地图的关键和价值

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

并没有为我工作了。 我现在得到这样的信息:属性或字段“标题”不能在空 找到我编辑接收器以这样的: :片段=“接收器(地图)”个:每= “条目:$ {地图}”> <标题日:文本= “$ {entry.title}”>标题 <体日:文本= “$ {entry.body}”> 体 –

+0

NOT $ {entry.title}。使用$ {entry.key} –

+0

Map(Key,Value)not Map(title,body) –

0

所以我想出了属性文件被处理为Map。所以'网站'根本不是一张地图。

我现在的解决方案是传递变量prefi的名称,并通过thymeleaf预处理获得密钥。

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