我有一个类任务,其中包含一个预订列表。
public class Task extends BaseTask {
@ManyToOne
private User assignee;
@OneToMany(cascade = CascadeType.ALL, orphanRemoval = true, mappedBy = "task")
private List<Booking> times = new ArrayList<>();
}
public class Booking {
@ManyToOne(optional = false)
private User user;
@ManyToOne(optional = false)
private Task task;
private int timeSpent;
}在控制器中,我将任务和当前登录的用户添加到模型中。
@GetMapping("/viewTask/{taskId}")
public String viewTask(@PathVariable("taskId") Long taskI, Model model)
model.addAttribute("task", taskService.findById(taskId));
model.addAttribute("userObject", userService.findCurrentUser());
}现在,我要计算当前登录的用户已经在任务对象上预订了多少时间。为了做到这一点,我想使用胸腺叶aggregates.sum函数。
此语句将所有预订的timeSpent相加。
th:text="${#aggregates.sum(task.times.![timeSpent])} + ' hours'"但我只想添加timeSpent,如果预订属于当前登录的用户。如果我硬编码用户的名字(唯一的)来检查预订是否属于他,它也可以工作。
th:text="${#aggregates.sum(task.times.?[user.getName() == 'sandra'].![timeSpent])} + ' hours'"但是如果我尝试为条件使用model属性,如下所示:
th:text="${#aggregates.sum(task.times.?[user == ${userObject}].![timeSpent])} + ' hours'"
or
th:text="${#aggregates.sum(task.times.?[user == __${userObject}__].![timeSpent])} + ' hours'"我得到了以下异常:
Property or field 'sandra' cannot be found on object of type 'de.hsba.bi.projectwork.booking.Booking' - maybe not public or not valid?如果我尝试像这样比较ids:
th:text="${#aggregates.sum(task.times.?[user.id == ${userObject.id}].![timeSpent])} + ' hours'"
th:text="${#aggregates.sum(task.times.?[user.getId() == ${userObject.getId()}].![timeSpent])} + ' hours'"抛出此异常:
Expression [#aggregates.sum(task.times.?[user.id == ${userObject.id}].![timeSpent])] @41: EL1043E: Unexpected token. Expected 'rsquare(])' but was 'lcurly({)'有人知道我做错了什么吗?
发布于 2020-10-20 15:56:03
我修复了这个问题,首先声明了一个胸腺叶变量,如下所示:
th:with="userId = ${userObject.getId()}"然后像这样做比较
th:text="${#aggregates.sum(task.times.?[user.id == __${userId}__].![timeSpent])} + ' hours'"最后,我的html看起来像这样:
<td th:with="userId = ${userObject.getId()}">
<b th:text="${#aggregates.sum(task.times.?[user.id == __${userId}__].![timeSpent])} + ' hours'"></b>
</td>https://stackoverflow.com/questions/64429635
复制相似问题