英文:
Using a conditional expression in thymeleaf aggregates.sum
问题
我有一个名为Task的类,它保存着一个预订(bookings)列表。
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;
}
在控制器中,我将任务(task)和当前登录的用户添加到模型中。
@GetMapping("/viewTask/{taskId}")
public String viewTask(@PathVariable("taskId") Long taskId, Model model) {
model.addAttribute("task", taskService.findById(taskId));
model.addAttribute("userObject", userService.findCurrentUser());
}
现在,我想计算当前登录的用户已经在任务对象上预订了多少时间。为此,我想使用Thymeleaf的aggregates.sum
函数。
以下语句将所有预订(bookings)的timeSpent
时间累加起来。
th:text="${#aggregates.sum(task.times.![timeSpent])} + ' hours'"
但我只想在预订属于当前登录用户时累加timeSpent
时间。如果我硬编码用户名(唯一值)来检查预订是否属于该用户,也可以正常工作。
th:text="${#aggregates.sum(task.times.?[user.getName() == 'sandra'].![timeSpent])} + ' hours'"
但是,如果我尝试使用模型属性作为条件,像这样:
th:text="${#aggregates.sum(task.times.?[user == ${userObject}].![timeSpent])} + ' hours'"
或者
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?
如果我尝试像这样比较ID:
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({)}'
有人知道我哪里出错了吗?
英文:
I have a class Task which holds a list of bookings.
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;
}
In the controller I add the task and the user which is currently logged in to the model.
@GetMapping("/viewTask/{taskId}")
public String viewTask(@PathVariable("taskId") Long taskI, Model model)
model.addAttribute("task", taskService.findById(taskId));
model.addAttribute("userObject", userService.findCurrentUser());
}
Now I want calculate how much time the currently logged in user has already booked on the task object. To do so I´d like to use thymeleafs aggregates.sum function.
This statement adds up the timeSpent of ALL bookings.
th:text="${#aggregates.sum(task.times.![timeSpent])} + ' hours'"
But I ONLY want to add the timeSpent if the booking belongs to the currently logged in user. If I hard code the name of the user (unique) to check if the booking belongs to him, it works as well.
th:text="${#aggregates.sum(task.times.?[user.getName() == 'sandra'].![timeSpent])} + ' hours'"
But if I try to use the model attribute for the condition like so:
th:text="${#aggregates.sum(task.times.?[user == ${userObject}].![timeSpent])} + ' hours'"
or
th:text="${#aggregates.sum(task.times.?[user == __${userObject}__].![timeSpent])} + ' hours'"
I get the following exception:
Property or field 'sandra' cannot be found on object of type 'de.hsba.bi.projectwork.booking.Booking' - maybe not public or not valid?
If I try comparing the ids like this:
th:text="${#aggregates.sum(task.times.?[user.id == ${userObject.id}].![timeSpent])} + ' hours'"
th:text="${#aggregates.sum(task.times.?[user.getId() == ${userObject.getId()}].![timeSpent])} + ' hours'"
This exception is thrown:
Expression [#aggregates.sum(task.times.?[user.id == ${userObject.id}].![timeSpent])] @41: EL1043E: Unexpected token. Expected 'rsquare(])' but was 'lcurly({)'
Does someone know what I´m doing wrong?
答案1
得分: 0
我通过首先像这样声明一个 Thymeleaf 变量来修复了这个问题:
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>
英文:
I fixed the issue by first declaring a thymeleaf-variable like so:
th:with="userId = ${userObject.getId()}"
and then doing the comparison like so
th:text="${#aggregates.sum(task.times.?[user.id == __${userId}__].![timeSpent])} + ' hours'"
In the end my html looks like this:
<td th:with="userId = ${userObject.getId()}">
<b th:text="${#aggregates.sum(task.times.?[user.id == __${userId}__].![timeSpent])} + ' hours'"></b>
</td>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论