在Thymeleaf中使用条件表达式的aggregates.sum

huangapple go评论67阅读模式
英文:

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 = &quot;task&quot;)
    private List&lt;Booking&gt; times = new ArrayList&lt;&gt;();

}


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(&quot;/viewTask/{taskId}&quot;)
    public String viewTask(@PathVariable(&quot;taskId&quot;) Long taskI, Model model) 
        model.addAttribute(&quot;task&quot;, taskService.findById(taskId));
        model.addAttribute(&quot;userObject&quot;, 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=&quot;${#aggregates.sum(task.times.![timeSpent])} + &#39; hours&#39;&quot;

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=&quot;${#aggregates.sum(task.times.?[user.getName() == &#39;sandra&#39;].![timeSpent])} + &#39; hours&#39;&quot;

But if I try to use the model attribute for the condition like so:

th:text=&quot;${#aggregates.sum(task.times.?[user == ${userObject}].![timeSpent])} + &#39; hours&#39;&quot;
or
th:text=&quot;${#aggregates.sum(task.times.?[user == __${userObject}__].![timeSpent])} + &#39; hours&#39;&quot;

I get the following exception:

Property or field &#39;sandra&#39; cannot be found on object of type &#39;de.hsba.bi.projectwork.booking.Booking&#39; - maybe not public or not valid?

If I try comparing the ids like this:

th:text=&quot;${#aggregates.sum(task.times.?[user.id == ${userObject.id}].![timeSpent])} + &#39; hours&#39;&quot;
th:text=&quot;${#aggregates.sum(task.times.?[user.getId() == ${userObject.getId()}].![timeSpent])} + &#39; hours&#39;&quot;

This exception is thrown:

Expression [#aggregates.sum(task.times.?[user.id == ${userObject.id}].![timeSpent])] @41: EL1043E: Unexpected token. Expected &#39;rsquare(])&#39; but was &#39;lcurly({)&#39;

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=&quot;userId = ${userObject.getId()}&quot;

and then doing the comparison like so

th:text=&quot;${#aggregates.sum(task.times.?[user.id == __${userId}__].![timeSpent])} + &#39; hours&#39;&quot;

In the end my html looks like this:

&lt;td th:with=&quot;userId = ${userObject.getId()}&quot;&gt;
&lt;b th:text=&quot;${#aggregates.sum(task.times.?[user.id == __${userId}__].![timeSpent])} + &#39; hours&#39;&quot;&gt;&lt;/b&gt;
&lt;/td&gt;

huangapple
  • 本文由 发表于 2020年10月19日 22:38:32
  • 转载请务必保留本文链接:https://go.coder-hub.com/64429635.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定