gpt4 book ai didi

java - 是否可以在 Play 中使用 Java 常量?模板?

转载 作者:行者123 更新时间:2023-12-04 06:07:21 24 4
gpt4 key购买 nike

我有一个用户实体:

@Entity
public class User extends PortalModel {

...

public enum Role {
User,
Admin,
}

@Required
public Role role;

public boolean isAdmin() {
return role == Role.Admin;
}
}

我尝试使用这个模板:
#{if user && user.role == User.Role.Admin}
<li>
<a href="/admin">Admin</a>
</li>
#{/if}

这失败了,我怀疑原因是您只能从模板访问模型对象本身,而不能访问其他运行时类,甚至包括枚举常量。我开始定义和使用 isAdmin() 方法:
#{if user && user.isAdmin()}

这奏效了。我想我是对的,但只是想确认一下 - 您可以从模板访问的唯一 Java 对象是模型类本身,它们在 render() 中传递吗?或输入 renderArgs ?

最佳答案

template engine docs为......而玩!说表达式语法是 Groovy,所以这应该是可能的。尝试使用 User 的完全限定名称.

关于java - 是否可以在 Play 中使用 Java 常量?模板?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8201660/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com