遍历Thymeleaf中的地图列表

时间:2019-03-25 20:55:27

标签: java spring-boot thymeleaf

我有一个包含这样的地图的列表:

Map<String, Long> count = new HashMap<>();
count.put("totalMessageCount", 5L);         
Map<String, Map<String, Long>> names = new HashMap<>();
names.put("someKey", count);
List<Map<String, Map<String, Long>>> list = new ArrayList<>();
list.add(names);

我正在将此列表从控制器发送到视图。

我已经尝试过了:

<table>
        <tr th:each="element : ${list}">
        <td th:text="${element.key}"></td>
        <td th:text="${element.value}"></td>
</table>

我得到一个错误:

  

org.springframework.expression.spel.SpelEvaluationException:EL1008E:在类型'java.util.HashMap'的对象上找不到属性或字段'key'-可能不是公共的或无效的吗?       在org.springframework.expression.spel.ast.PropertyOrFieldReference.readProperty(PropertyOrFieldReference.java:217)       在org.springframework.expression.spel.ast.PropertyOrFieldReference.getValueInternal(PropertyOrFieldReference.java:104)       在org.springframework.expression.spel.ast.PropertyOrFieldReference.access处$ 000(PropertyOrFieldReference.java:51)       在org.springframework.expression.spel.ast.PropertyOrFieldReference $ AccessorLValue.getValue(PropertyOrFieldReference.java:406)       在org.springframework.expression.spel.ast.CompoundExpression.getValueInternal(CompoundExpression.java:90)       在org.springframework.expression.spel.ast.SpelNodeImpl.getValue(SpelNodeImpl.java:109)       在org.springframework.expression.spel.standard.SpelExpression.getValue(SpelExpression.java:328)       在org.thymeleaf.spring5.expression.SPELVariableExpressionEvaluator.evaluate(SPELVariableExpressionEvaluator.java:263)       在org.thymeleaf.standard.expression.VariableExpression.executeVariableExpression(VariableExpression.java:166)       在org.thymeleaf.standard.expression.SimpleExpression.executeSimple(SimpleExpression.java:66)       在org.thymeleaf.standard.expression.Expression.execute(Expression.java:109)       在org.thymeleaf.standard.expression.AdditionExpression.executeAddition(AdditionExpression.java:96)       在org.thymeleaf.standard.expression.ComplexExpression.executeComplex(ComplexExpression.java:62)       在org.thymeleaf.standard.expression.Expression.execute(Expression.java:112)       在org.thymeleaf.standard.expression.Expression.execute(Expression.java:138)       在org.thymeleaf.standard.processor.AbstractStandardExpressionAttributeTagProcessor.doProcess(AbstractStandardExpressionAttributeTagProcessor.java:144)       在org.thymeleaf.processor.element.AbstractAttributeTagProcessor.doProcess(AbstractAttributeTagProcessor.java:74)       在org.thymeleaf.processor.element.AbstractElementTagProcessor.process(AbstractElementTagProcessor.java:95)       在org.thymeleaf.util.ProcessorConfigurationUtils $ ElementTagProcessorWrapper.process(ProcessorConfigurationUtils.java:633)       在org.thymeleaf.engine.ProcessorTemplateHandler.handleOpenElement(ProcessorTemplateHandler.java:1314)       在org.thymeleaf.engine.OpenElementTag.beHandled(OpenElementTag.java:205)       在org.thymeleaf.engine.Model.process(Model.java:282)       在org.thymeleaf.engine.Model.process(Model.java:290)       在org.thymeleaf.engine.IteratedGatheringModelProcessable.processIterationModel(IteratedGatheringModelProcessable.java:367)       在org.thymeleaf.engine.IteratedGatheringModelProcessable.process(IteratedGatheringModelProcessable.java:221)处       在org.thymeleaf.engine.ProcessorTemplateHandler.handleCloseElement(ProcessorTemplateHandler.java:1640)       在org.thymeleaf.engine.CloseElementTag.beHandled(CloseElementTag.java:139)       在org.thymeleaf.engine.Model.process(Model.java:282)       在org.thymeleaf.engine.Model.process(Model.java:290)       在org.thymeleaf.engine.IteratedGatheringModelProcessable.processIterationModel(IteratedGatheringModelProcessable.java:367)       在org.thymeleaf.engine.IteratedGatheringModelProcessable.process(IteratedGatheringModelProcessable.java:221)处       在org.thymeleaf.engine.ProcessorTemplateHandler.handleCloseElement(ProcessorTemplateHandler.java:1640)       在org.thymeleaf.engine.CloseElementTag.beHandled(CloseElementTag.java:139)       在org.thymeleaf.engine.Model.process(Model.java:282)       在org.thymeleaf.engine.Model.process(Model.java:290)       在org.thymeleaf.engine.IteratedGatheringModelProcessable.processIterationModel(IteratedGatheringModelProcessable.java:367)       在org.thymeleaf.engine.IteratedGatheringModelProcessable.process(IteratedGatheringModelProcessable.java:221)处       在org.thymeleaf.engine.ProcessorTemplateHandler.handleCloseElement(ProcessorTemplateHandler.java:1640)       在org.thymeleaf.engine.CloseElementTag.beHandled(CloseElementTag.java:139)       在org.thymeleaf.engine.TemplateModel.process(TemplateModel.java:136)       在org.thymeleaf.engine.TemplateManager.parseAndProcess(TemplateManager.java:661)       在org.thymeleaf.TemplateEngine.process(TemplateEngine.java:1098)       在org.thymeleaf.TemplateEngine.process(TemplateEngine.java:1072)       在org.thymeleaf.spring5.view.ThymeleafView.renderFragment(ThymeleafView.java:362)       在org.thymeleaf.spring5.view.ThymeleafView.render(ThymeleafView.java:189)       在org.springframework.web.servlet.DispatcherServlet.render(DispatcherServlet.java:1370)       在org.springframework.web.servlet.DispatcherServlet.processDispatchResult(DispatcherServlet.java:1116)       在org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:1055)       在org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:942)       在org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:1005)       在org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:897)       在javax.servlet.http.HttpServlet.service(HttpServlet.java:634)       在org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:882)       在javax.servlet.http.HttpServlet.service(HttpServlet.java:741)       在org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231)       在org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)       在org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)       在org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)       在org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)       在org.springframework.web.filter.RequestContextFilter.doFilterInternal(RequestContextFilter.java:99)       在org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)       在org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)       在org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)       在org.springframework.web.filter.FormContentFilter.doFilterInternal(FormContentFilter.java:92)       在org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)       在org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)       在org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)       在org.springframework.web.filter.HiddenHttpMethodFilter.doFilterInternal(HiddenHttpMethodFilter.java:93)       在org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)       在org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)       在org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)       在org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:200)       在org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)       在org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)       在org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)       在org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:200)       在org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96)       在org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:490)       在org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:139)       在org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:92)       在org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:74)       在org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:668)       在org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:343)       在org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:408)       在org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66)       在org.apache.coyote.AbstractProtocol $ ConnectionHandler.process(AbstractProtocol.java:834)       在org.apache.tomcat.util.net.NioEndpoint $ SocketProcessor.doRun(NioEndpoint.java:1415)       在org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)       在java.base / java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1135)       在java.base / java.util.concurrent.ThreadPoolExecutor $ Worker.run(ThreadPoolExecutor.java:635)       在org.apache.tomcat.util.threads.TaskThread $ WrappingRunnable.run(TaskThread.java:61)       在java.base / java.lang.Thread.run(Thread.java:844)

感谢您的帮助。

1 个答案:

答案 0 :(得分:2)

您很有可能需要另一层嵌套。

<table>
    <th:block th:each="map : ${list}">
       <tr th:each="e : ${map}">
          <td th:text="${e.key}"></td>
          <td th:text="${e.value}"></td>
       </tr>
    </th:block>
</table>

keyvalueMap.Entry的属性。