如何使用Thymeleaf从集合中获取信息

时间:2014-01-10 13:28:02

标签: spring spring-mvc thymeleaf

我的控制器看起来像这样:

@ModelAttribute("adminMenu")
public Collection<Menu> getAdminMenu(HttpSession session, HttpServletRequest request) {
    Collection<Menu> menuCollection = this.stormfallService.findMenuByType("A", "A");
    log.debug("The collection for admin has " + menuCollection.size() + " records");
    session.setAttribute("adminMenu", menuCollection);
    /*Menu menu;
    Iterator<Menu> itr = menuCollection.iterator();
    while (itr.hasNext()) {
        menu = (Menu)itr.next();
        log.debug("The id = " + menu.getId());
        log.debug("The menu text is " + menu.getMenuText());
        log.debug("The menu type is " + menu.getMenuType());
        log.debug("The menu url is " + menu.getMenuUrl());
        log.debug("The menu sort order is " + menu.getSortOrder());
        log.debug("The menu status is " + menu.getStatus());
    }*/

    return menuCollection;
    //return null;
}

我认为这是正确的,但如果没有,请告诉我它应该是什么。

我的片段看起来像这样:

<div th:fragment="sidebar" class="sidebar" id="textbar">
    <h4>Admin Functions</h4>
            <ul class="menu">
                    <!-- li th:each="menu,rowStat : ${adminMenu}" -->
                    <li th:each="menu,rowStat : #{session.adminMenu}">
                    <a href="#" th:href="${menu.menuUrl}" th:text="${menu.menuText}">Some Admin Text</a></li>
            </ul>
    <h4>User Functions</h4>
            <ul class="menu">
                    <!-- li th:each="menu,rowStat : ${userMenu}" -->
                    <li th:each="menu,rowStat : #{session.userMenu">
                    <a href="#" th:href="${menu.menuUrl}" th:text="${menu.menuText}">Some User Text</a></li>
            </ul>
    <h4>Tools</h4>
            <ul class="menu">
                <li><a href="#">Change Password</a></li>
                <li><a href="#">Logout</a></li>
            </ul>
</div>

当我没有使用会话时,注释掉的代码会构建菜单,但是当它转到其他页面时,菜单会消失。所以它需要在会议中。

这个新代码出现以下错误:

HTTP状态500 - 请求处理失败;嵌套异常是org.thymeleaf.exceptions.TemplateProcessingException:评估SpringEL表达式的异常:“menu.menuUrl”(layout:141)

输入例外报告

消息请求处理失败;嵌套异常是org.thymeleaf.exceptions.TemplateProcessingException:评估SpringEL表达式的异常:“menu.menuUrl”(layout:141)

说明服务器遇到内部错误,导致无法完成此请求。

exception

org.springframework.web.util.NestedServletException: Request processing failed; nested exception is org.thymeleaf.exceptions.TemplateProcessingException: Exception evaluating SpringEL expression: "menu.menuUrl" (layout:141)
    org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:948)
    org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:827)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:621)
    org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:812)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
    org.springframework.web.filter.HiddenHttpMethodFilter.doFilterInternal(HiddenHttpMethodFilter.java:77)
    org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)

root cause

org.thymeleaf.exceptions.TemplateProcessingException: Exception evaluating SpringEL expression: "menu.menuUrl" (layout:141)
    org.thymeleaf.spring3.expression.SpelVariableExpressionEvaluator.evaluate(SpelVariableExpressionEvaluator.java:153)
    org.thymeleaf.standard.expression.VariableExpression.executeVariable(VariableExpression.java:149)
    org.thymeleaf.standard.expression.SimpleExpression.executeSimple(SimpleExpression.java:59)
    org.thymeleaf.standard.expression.Expression.execute(Expression.java:103)
    org.thymeleaf.standard.expression.Expression.execute(Expression.java:133)
    org.thymeleaf.standard.expression.Expression.execute(Expression.java:120)
    org.thymeleaf.standard.processor.attr.AbstractStandardSingleAttributeModifierAttrProcessor.getTargetAttributeValue(AbstractStandardSingleAttributeModifierAttrProcessor.java:67)
    org.thymeleaf.spring3.processor.attr.SpringHrefAttrProcessor.getTargetAttributeValue(SpringHrefAttrProcessor.java:68)
    org.thymeleaf.processor.attr.AbstractSingleAttributeModifierAttrProcessor.getModifiedAttributeValues(AbstractSingleAttributeModifierAttrProcessor.java:59)
    org.thymeleaf.processor.attr.AbstractAttributeModifierAttrProcessor.processAttribute(AbstractAttributeModifierAttrProcessor.java:61)
    org.thymeleaf.processor.attr.AbstractAttrProcessor.doProcess(AbstractAttrProcessor.java:87)
    org.thymeleaf.processor.AbstractProcessor.process(AbstractProcessor.java:212)
    org.thymeleaf.dom.Node.applyNextProcessor(Node.java:973)
    org.thymeleaf.dom.Node.processNode(Node.java:928)
    org.thymeleaf.dom.NestableNode.computeNextChild(NestableNode.java:654)
    org.thymeleaf.dom.NestableNode.doAdditionalProcess(NestableNode.java:637)
    org.thymeleaf.dom.Node.processNode(Node.java:947)
    org.thymeleaf.dom.NestableNode.computeNextChild(NestableNode.java:654)
    org.thymeleaf.dom.NestableNode.doAdditionalProcess(NestableNode.java:637)
    org.thymeleaf.dom.Node.processNode(Node.java:947)
    org.thymeleaf.dom.NestableNode.computeNextChild(NestableNode.java:654)
    org.thymeleaf.dom.NestableNode.doAdditionalProcess(NestableNode.java:637)
    org.thymeleaf.dom.Node.processNode(Node.java:947)
    org.thymeleaf.dom.NestableNode.computeNextChild(NestableNode.java:654)
    org.thymeleaf.dom.NestableNode.doAdditionalProcess(NestableNode.java:637)
    org.thymeleaf.dom.Node.processNode(Node.java:947)
    org.thymeleaf.dom.NestableNode.computeNextChild(NestableNode.java:654)
    org.thymeleaf.dom.NestableNode.doAdditionalProcess(NestableNode.java:637)
    org.thymeleaf.dom.Node.processNode(Node.java:947)
    org.thymeleaf.dom.NestableNode.computeNextChild(NestableNode.java:654)
    org.thymeleaf.dom.NestableNode.doAdditionalProcess(NestableNode.java:637)
    org.thymeleaf.dom.Node.processNode(Node.java:947)
    org.thymeleaf.dom.NestableNode.computeNextChild(NestableNode.java:654)
    org.thymeleaf.dom.NestableNode.doAdditionalProcess(NestableNode.java:637)
    org.thymeleaf.dom.Node.processNode(Node.java:947)
    org.thymeleaf.dom.Document.process(Document.java:93)
    org.thymeleaf.TemplateEngine.process(TemplateEngine.java:1155)
    org.thymeleaf.TemplateEngine.process(TemplateEngine.java:1060)
    org.thymeleaf.TemplateEngine.process(TemplateEngine.java:1011)
    org.thymeleaf.spring3.view.ThymeleafView.renderFragment(ThymeleafView.java:335)
    org.thymeleaf.spring3.view.ThymeleafView.render(ThymeleafView.java:190)
    org.springframework.web.servlet.DispatcherServlet.render(DispatcherServlet.java:1208)
    org.springframework.web.servlet.DispatcherServlet.processDispatchResult(DispatcherServlet.java:992)
    org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:939)
    org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:856)
    org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:936)
    org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:827)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:621)
    org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:812)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
    org.springframework.web.filter.HiddenHttpMethodFilter.doFilterInternal(HiddenHttpMethodFilter.java:77)
    org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)

root cause

org.springframework.expression.spel.SpelEvaluationException: EL1008E:(pos 5): Field or property 'menuUrl' cannot be found on object of type 'java.lang.String'
    org.springframework.expression.spel.ast.PropertyOrFieldReference.readProperty(PropertyOrFieldReference.java:246)
    org.springframework.expression.spel.ast.PropertyOrFieldReference.getValueInternal(PropertyOrFieldReference.java:112)
    org.springframework.expression.spel.ast.PropertyOrFieldReference.access$000(PropertyOrFieldReference.java:43)
    org.springframework.expression.spel.ast.PropertyOrFieldReference$AccessorLValue.getValue(PropertyOrFieldReference.java:87)
    org.springframework.expression.spel.ast.CompoundExpression.getValueInternal(CompoundExpression.java:82)
    org.springframework.expression.spel.ast.SpelNodeImpl.getValue(SpelNodeImpl.java:93)
    org.springframework.expression.spel.standard.SpelExpression.getValue(SpelExpression.java:94)
    org.thymeleaf.spring3.expression.SpelVariableExpressionEvaluator.evaluate(SpelVariableExpressionEvaluator.java:138)
    org.thymeleaf.standard.expression.VariableExpression.executeVariable(VariableExpression.java:149)
    org.thymeleaf.standard.expression.SimpleExpression.executeSimple(SimpleExpression.java:59)
    org.thymeleaf.standard.expression.Expression.execute(Expression.java:103)
    org.thymeleaf.standard.expression.Expression.execute(Expression.java:133)
    org.thymeleaf.standard.expression.Expression.execute(Expression.java:120)
    org.thymeleaf.standard.processor.attr.AbstractStandardSingleAttributeModifierAttrProcessor.getTargetAttributeValue(AbstractStandardSingleAttributeModifierAttrProcessor.java:67)
    org.thymeleaf.spring3.processor.attr.SpringHrefAttrProcessor.getTargetAttributeValue(SpringHrefAttrProcessor.java:68)
    org.thymeleaf.processor.attr.AbstractSingleAttributeModifierAttrProcessor.getModifiedAttributeValues(AbstractSingleAttributeModifierAttrProcessor.java:59)
    org.thymeleaf.processor.attr.AbstractAttributeModifierAttrProcessor.processAttribute(AbstractAttributeModifierAttrProcessor.java:61)
    org.thymeleaf.processor.attr.AbstractAttrProcessor.doProcess(AbstractAttrProcessor.java:87)
    org.thymeleaf.processor.AbstractProcessor.process(AbstractProcessor.java:212)
    org.thymeleaf.dom.Node.applyNextProcessor(Node.java:973)
    org.thymeleaf.dom.Node.processNode(Node.java:928)
    org.thymeleaf.dom.NestableNode.computeNextChild(NestableNode.java:654)
    org.thymeleaf.dom.NestableNode.doAdditionalProcess(NestableNode.java:637)
    org.thymeleaf.dom.Node.processNode(Node.java:947)
    org.thymeleaf.dom.NestableNode.computeNextChild(NestableNode.java:654)
    org.thymeleaf.dom.NestableNode.doAdditionalProcess(NestableNode.java:637)
    org.thymeleaf.dom.Node.processNode(Node.java:947)
    org.thymeleaf.dom.NestableNode.computeNextChild(NestableNode.java:654)
    org.thymeleaf.dom.NestableNode.doAdditionalProcess(NestableNode.java:637)
    org.thymeleaf.dom.Node.processNode(Node.java:947)
    org.thymeleaf.dom.NestableNode.computeNextChild(NestableNode.java:654)
    org.thymeleaf.dom.NestableNode.doAdditionalProcess(NestableNode.java:637)
    org.thymeleaf.dom.Node.processNode(Node.java:947)
    org.thymeleaf.dom.NestableNode.computeNextChild(NestableNode.java:654)
    org.thymeleaf.dom.NestableNode.doAdditionalProcess(NestableNode.java:637)
    org.thymeleaf.dom.Node.processNode(Node.java:947)
    org.thymeleaf.dom.NestableNode.computeNextChild(NestableNode.java:654)
    org.thymeleaf.dom.NestableNode.doAdditionalProcess(NestableNode.java:637)
    org.thymeleaf.dom.Node.processNode(Node.java:947)
    org.thymeleaf.dom.NestableNode.computeNextChild(NestableNode.java:654)
    org.thymeleaf.dom.NestableNode.doAdditionalProcess(NestableNode.java:637)
    org.thymeleaf.dom.Node.processNode(Node.java:947)
    org.thymeleaf.dom.Document.process(Document.java:93)
    org.thymeleaf.TemplateEngine.process(TemplateEngine.java:1155)
    org.thymeleaf.TemplateEngine.process(TemplateEngine.java:1060)
    org.thymeleaf.TemplateEngine.process(TemplateEngine.java:1011)
    org.thymeleaf.spring3.view.ThymeleafView.renderFragment(ThymeleafView.java:335)
    org.thymeleaf.spring3.view.ThymeleafView.render(ThymeleafView.java:190)
    org.springframework.web.servlet.DispatcherServlet.render(DispatcherServlet.java:1208)
    org.springframework.web.servlet.DispatcherServlet.processDispatchResult(DispatcherServlet.java:992)
    org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:939)
    org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:856)
    org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:936)
    org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:827)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:621)
    org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:812)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
    org.springframework.web.filter.HiddenHttpMethodFilter.doFilterInternal(HiddenHttpMethodFilter.java:77)
    org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)

note The full stack trace of the root cause is available in the Apache Tomcat/7.0.42 logs.
Apache Tomcat/7.0.42

任何帮助都将不胜感激。

1 个答案:

答案 0 :(得分:3)

您使用的是#{...}(国际化)语法,而不是${...}语法。

这也解释了以下异常,因为#{...}的结果是String

Field or property 'menuUrl' cannot be found on object of type 'java.lang.String'

#{session.adminMenu}#{session.userMenu(请注意缺失的})分别更改为${session.adminMenu}${session.userMenu}