首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >thymeleaf :每个都使用递增变量作为列表索引

thymeleaf :每个都使用递增变量作为列表索引
EN

Stack Overflow用户
提问于 2018-06-06 08:36:14
回答 2查看 1.2K关注 0票数 0

我有一门课程,上面有一份参与者名单。

public class course {

    private List<Address> attendeeList;

    public List<Address> getAttendeeList() {
        return attendeeList;
    }

}

如果我编辑现有课程,我希望选择字段自动选择正确参与者的选项。下面是我的html页面的摘录:

<p class="form-control-static" th:each="i, stat : ${course.attendeeList}"> 
   <select class="form-control" name="attendeeIds">
      <option th:value="0">Please select an Attendee</option>
      <option th:each="attendee : ${addresses}" 
         th:value="${attendee.id}" 
         th:text="${attendee.firstName}+' '+${attendee.lastName}" 
         th:selected="${attendee.id}==
            (course.attendeeList[${stat.index}].id})"> 
      </option>
   </select>
</p>

如果我打开页面,我得到以下错误:意外令牌。应为“rsquare(])”,但应为“lcurly({)”

如果我将${stat.index}替换为数字(例如0),并且我的课程有10名参与者,则选择的项目将出现10次,但始终选择第一个成员。如何在每个选择字段中选择正确的与会者?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2018-06-06 08:59:31

尝试从以下位置更改:

(course.attendeeList[${stat.index}].id})"> 

至:

(course.attendeeList[__${stat.index}__].id})"> 
票数 0
EN

Stack Overflow用户

发布于 2018-06-06 10:30:34

当您在表达式中时,您不需要使用另一个${...}。我会把这个表达式写成:

th:selected="${attendee.id == course.attendeeList[stat.index].id}"
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50710740

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档