- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在开发一个应用程序JAVA + Spring Boot + ThymeLeaf(简单且简单的前端仅用于测试)。我有简单的域类 Player:
@Entity
@Setter
@Getter
@EqualsAndHashCode(exclude = {"events"})
public class Player {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@Column(nullable = false)
private String playerName;
private String playerLastName;
private String playerNickname;
private String playerEmailAddress;
private String playerTelephoneNumber;
@ManyToMany(mappedBy = "players", fetch = FetchType.EAGER)
private Set<Event> events = new HashSet<>();
@Enumerated(value = EnumType.STRING)
private FavouritePosition favouritePosition;
private LocalDate birthdayDate;
}
我想计算“服务”类别中的玩家年龄。
问题是:如何将这些信息传输到前端?
Controller 方法如下所示:
@RequestMapping("/event/{eventId}/player/{playerId}/show")
public String showPlayerForSpecificTeam(Model model, @PathVariable String eventId, @PathVariable String playerId) {
model.addAttribute("player", playerService.getPlayerByEventIdAndPlayerId(Long.valueOf(eventId), Long.valueOf(playerId)));
return "event/player/showPlayerForSpecifiedEvent";
}
方法getPlayerByEventIdAndPlayerId(Long.valueOf(eventId), Long.valueOf(playerId))
返回Player对象(没有年龄,只有生日日期)。
我不想在数据库中存储每个玩家的年龄值 - 这是显而易见的。
我应该向 Controller 添加另一个:model.addAttribute();有年龄信息吗? (即使对我来说也看起来不太好)
我正在寻找聪明的解决方案,遵守干净代码和 MVC 的规则。如果您能帮助我找到好的解决方案,我将很高兴,谢谢。
编辑:我刚刚创建了 AgeCalculator 类:
@Component
@Data
public class AgeCalculator {
public Integer calculateAge(Player player) {
return Years.yearsBetween(player.getBirthdayDate(), new LocalDate()).getYears();
}
}
修改后的 Controller :
@RequestMapping({"player/{playerId}/show"})
public String getPlayerById(Model model, @PathVariable String playerId) {
model.addAttribute("player", playerService.getPlayerById(Long.valueOf(playerId)));
model.addAttribute("ageCalculator", new AgeCalculator());
return "event/player/showPlayerById";
}
在 thymeleaf 中我使用:
<p th:text="${ageCalculator.calculateAge(player)}">Here will be a Player age</p>
但我收到错误:org.thymeleaf.exceptions.TemplateProcessingException:评估 SpringEL 表达式时出现异常:“ageCalculator.calculateAge(player.birthdayDate)”
我的代码有什么问题吗?
最佳答案
我的首选方法是添加 getAge()
方法Player
类(class)。那么在前端,它看起来像这样:
<span th:text="${player.age}" />
如果您不想修改 Player
类,您可以使用 calculateAge(LocalDate birthday)
创建一个辅助对象方法,然后将其添加到模型中并像这样调用它:
Java:model.addAttribute("helper", new BirthdayHelper());
thymeleaf :<span th:text="${helper.calculateAge(player.birthdayDate)}" />
关于java - 如何传递信息,反对thymeleaf,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48407631/
我的查询不工作,它总是显示相关的 0。即使针对数据确实存在。 SELECT number, MATCH(number) AGAINST('02' '01' '03' WITH QUERY EXPA
我正在尝试对字段进行全文搜索以匹配字符串的特定部分。考虑一个包含数组值的长字符串,例如 201:::1###193:::5###193:::6###202:::6。 ### 分隔数组元素,::: 分隔
我正在构建一个音乐应用程序。在我的数据库中,我有一个“艺术家”表,与“专辑”表相关,与“轨道”表相关。 我的应用程序的每个用户都可以“喜欢”(竖起大拇指)或“不喜欢”(竖起大拇指)艺术家/专辑/轨道。
我正在使用这个搜索 -> 将 (c1, c2) 与 (">dg*"在 bool 模式下匹配); 我得到了这个列表 1 dgas 2 格卡 3 dgicb 4 天 5 位数 6 dgly 7 总督 8
我想从数据库中找到 date 为“2013-04”的所有记录。 我使用: SELECT date, pastabos FROM aktai WHERE MATCH(uzsakovas) AGAINST
我正在尝试为评论实现一个 upvote/downvote 机制(类似于 reddit 上的 upvote/downvote 机制)。我有一个名为 commentReputation 的单独集合里面的文
我从事安全关键应用程序开发工作。最近,作为一名代码审阅者,我提示如下所示的编码风格,但无法提出强有力的理由反对它。那么反对这种变量冗余/重复的一个很好的论据是什么,我正在寻找可能导致问题或可能失败的测
我有一个全文索引表,其中包含数据系列。没有空格,拼写正确。如何将此查询匹配结果作为分数并按分数按 DESC 顺序显示?我总是得到空集,不知道为什么。 SELECT *, MATCH(MEDIA_TI
编辑:改写问题。 除了不好的做法,还有什么其他原因反对 HTML 中的空段落? 原创: 背景 目前,要在我们的 CMS 中添加漂亮的空格段落,您需要按两次 Enter 键。我不喜欢空洞的段落,因为它们
我正在尝试将对象转换为 Json 字符串。我看到一个 TJson::ObjectToJsonString() 似乎可以满足需求,但似乎不起作用。我在 Delphi 中有确切的代码,没有问题。因此,显然
如果我想为 Linux 开发一个类似注册表的系统,我应该避免哪些 Windows 注册表设计失败? 哪些功能是绝对必要的? 主要问题是什么(安全性、易于配置……)? 我认为 Windows 注册表不是
自从 Adobe AIR 发布以来,我想知道为什么 Java Web Start 过去没有引起更多关注,因为在我看来它似乎非常相似,但 web start 可用的时间要长得多。 主要是因为 Sun
我经常想用一个假设 free_if_heap(void *ptr)函数,因为这会让我无需关心就返回 malloc 的、静态的或堆栈对象。例如 char *foo(int arg) { if (
关闭。这个问题需要更多focused .它目前不接受答案。 想改善这个问题吗?更新问题,使其仅关注一个问题 editing this post . 4年前关闭。 Improve this questi
我正在研究用 GCC 编译的 ARM C/C++ 代码的性能优化。 CPU 是 Tegra 3。 据我所知标志-mthumb表示生成旧的 16 位 Thumb 指令。在不同的测试中,-marm 的性能
As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be
我正要调试别人的代码,我偶然发现了一种处理全局数组的“方式”,我认为这种方式非常糟糕,但第一个使用它的人发誓要这样做。我需要找到反对它的论据。这是简化后的代码(这不是原始代码,只是一个抽象版本) 所以
select col1,col2,col3,col4,col5 from table1 where (col1 like '%keyword%' or col2 like '%keywo
我发现我经常进行递归调用只是为了重新排列参数。 例如,这是我对 endOther from codingbat.com 的解决方案: Given two strings, return true if
首先,我知道在复制文件时,如果缓冲区大小越大,复制过程就会越快。 以下 ruby 脚本用于逐行编写(即缓冲区的大小将取决于行的长度): input,output = ARGV[0], ARGV[1]
我是一名优秀的程序员,十分优秀!