Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/spring-mvc/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Spring mvc Thymeleaf映射语法导致TemplateProcessingException_Spring Mvc_Thymeleaf - Fatal编程技术网

Spring mvc Thymeleaf映射语法导致TemplateProcessingException

Spring mvc Thymeleaf映射语法导致TemplateProcessingException,spring-mvc,thymeleaf,Spring Mvc,Thymeleaf,我正在尝试构建一个包含用户及其权限的网格页面。该项目是一个SpringMVCBoot+Thymeleaf项目。版本: Thymeleaf 3.0.3 Thymeleaf布局方言2.2.1 弹簧靴1.5.2 (注意,我必须覆盖Spring Boot的Thymeleaf版本,因为 布局方言版本已经非常过时。) 每个用户都有一个办公室和多个角色。RoleDescription、Office和User是包含多个字符串的POJO。我像这样在控制器中填充数据 @RequestMapping("/admi

我正在尝试构建一个包含用户及其权限的网格页面。该项目是一个SpringMVCBoot+Thymeleaf项目。版本:

  • Thymeleaf 3.0.3
  • Thymeleaf布局方言2.2.1
  • 弹簧靴1.5.2 (注意,我必须覆盖Spring Boot的Thymeleaf版本,因为 布局方言版本已经非常过时。)
  • 每个用户都有一个办公室和多个角色。RoleDescription、Office和User是包含多个字符串的POJO。我像这样在控制器中填充数据

      @RequestMapping("/admin/users")
      public String userGrid(Model model) {
          MyWebServiceClient myClient = clientFactory.getClient();
          List<RoleDescription> roles = myClient.getRoleList();
          Map<String, Office> offices = myClient.getOfficeMap();
          List<User> users = myClient.getUserRoleGrid();
          model.addAttribute("offices", offices);
          model.addAttribute("roles", roles);
          model.addAttribute("users", users);
          return "usergrid";
      }
    
    @RequestMapping(“/admin/users”)
    公共字符串userGrid(模型){
    MyWebServiceClient myClient=clientFactory.getClient();
    List roles=myClient.getRoleList();
    Map offices=myClient.getOfficeMap();
    List users=myClient.getUserRoleGrid();
    model.addAttribute(“办公室”,办公室);
    model.addAttribute(“角色”,角色);
    model.addAttribute(“用户”,用户);
    返回“usergrid”;
    }
    
    然后,我在my Thymeleaf模板页面中显示数据,如下所示:

            <tbody th:each="user: ${users}" th:with="officeId=${user.officeId},office=${offices[officeId]}">
                <tr>
                    <td th:text="${user.fullName}">Fred User</td>
                    <td th:text="${user.userId}">fred</td>
                    <td th:text="${office.name} (${officeId})">Madison, Wisconsin (madison)</td>
                    <th:block th:each="role: ${roles}">
                        <td th:text="${#lists.contains(user.roles, role)} ? 'yes'"></td>
                    </th:block>
                </tr>
            </tbody>
    
    
    弗雷德用户
    弗雷德
    威斯康星州麦迪逊(麦迪逊)
    
    我尝试了几种不同的
    offices[officeId]
    ,试图从Map对象检索office名称。表达式的完整形式(不带
    th:with
    语句)看起来像
    offices[user.officeId].name
    。我已经转储了office地图,以确保它不是空的,并且它充满了数据

    无论我做什么,我都会得到一些变化:

    org.thymeleaf.exceptions.TemplateProcessingException:无法作为表达式解析:“${office}(${officeId})”


    只是一个评论:在与Freemarker合作几年后,它已经足够成熟,可以给你几乎超乎寻常的有洞察力的错误消息,这是非常恼人的

    我不认为这里的问题是
    th:with
    。事实上,这句话:
    威斯康星州麦迪逊(麦迪逊)
    与错误消息相匹配。我建议将其更改为类似以下内容以修复/使其更具可读性:

    <td>
        <span th:text="${office.name}" /> (<span th:text="${officeId}" />)
    </td>
    
    
    ()
    
    或者保留原始语法

    <td th:text="${office.name + ' (' + officeId + ')'}">Madison, Wisconsin (madison)</td>
    
    威斯康星州麦迪逊(麦迪逊)
    
    总而言之:我不应该把括号塞进里面。我想我有暂时的括号盲症。我还发现“with”在这种情况下是无用的。它将office的值设置为列表中的第一个值,并且不会为每一行更新。@k-den
    th:with
    应按您希望的方式使用
    th:each
    。。。我刚刚用自己的测试用例进行了验证。您是否可以在其他地方覆盖该变量?