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
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
@(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
}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
Assuming User is null if there's no user, shouldn't you have
@(user: User)
@if(user!=null){
// do stuff
} else {
// do stuff for no user
}
In scala homepage template am trying something like this
@(user: User)
@if(user.fname==null){ i even tried @if(user.isEmpty)
If user can be null, use Option[User] instead of User. Inside a template you can use fold in order to handle Option.
In Scala is a bad practice to use null. If a variable can be null, use Option instead, let it be known that a variable can be null, expressivity over all.
@(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>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.