[英]if statement in scala template play framework
hey guys am trying to send a value for user if he is logged in and in not i execute an if statement in a home page scala template 嘿,如果他已经登录,我正在尝试为用户发送一个值,而不是我在首页scala模板中执行if语句
User loggeduser = User.find.query().where().eq("token",
session("connected")).findOne();
return ok(main.render(loggeduser));
in scala home page template am trying something like this 在scala主页模板中,我正在尝试类似这样的操作
@(user: User)
@if(user.fname==null){ i even tried @if(user.isEmpty)
and tried to keep fname outside yet the same error for a NULL POINTER EXEPTION then show some login form links 并尝试将fname保留在NULL POINTER EXEPTION之外但仍然相同的错误之外,然后显示一些登录表单链接
}else(user!=null){
<a class="dropdown-toggle" data-toggle="dropdown" id="signHover">@user.fname</a>
} }
please if have any suggestion guide me throw the process 如果有任何建议请指导我抛出过程
reference : https://www.playframework.com/documentation/2.6.x/JavaTemplates 参考: https : //www.playframework.com/documentation/2.6.x/JavaTemplates
Assuming User is null if there's no user, shouldn't you have 假设如果没有用户,则User为null,是否应该
@(user: User)
@if(user!=null){
// do stuff
} else {
// do stuff for no user
}
In scala homepage template am trying something like this 在Scala主页模板中,我正在尝试类似的操作
@(user: User)
@if(user.fname==null){ i even tried @if(user.isEmpty)
If user can be null, use Option[User] instead of User. 如果user可以为null,请使用Option [User]代替User。 Inside a template you can use fold in order to handle Option.
在模板内部,您可以使用fold来处理Option。
In Scala is a bad practice to use null. 在Scala中,不建议使用null。 If a variable can be null, use Option instead, let it be known that a variable can be null, expressivity over all.
如果变量可以为null,请改用Option,让它知道变量可以为null,并在所有方面表现出来。
@(userOpt:Option[User])
<div>SOME HTML</div>
@userOpt.fold {
<div>USER ISN'T LOGGED</div>
} { user =>
<div>USER IS LOGGED! @user</div>
}
<div>SOME HTML</div>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.