JSF: Invoke method on returned object of a method
-
16-06-2021 - |
Question
I've stumbled upon a problem I don't seem to be able to solve on my own with my basic knowledge of jsf.
I have a session scoped object which is referenced by userSession.
So if I want to get the property loginTimestamp I just call userSession.loginTimestamp which is no problem and works perfectly fine.
Now I also have a method getLoggedInUser which gives me an Object of the type user. On that object I can call a method getNickname which should give me a string.
The problem is that I want to call something like
<div id="login">
<h:outputText value="Welcome #{userSession.getLoggedInUser.getNickname}" />
<h:form id="logoutForm">
<h:commandButton class="Submit" value="logout"
action="#{Logout.logout}" />
</h:form>
</div>
No the method getLoggedInUser has no property loggedInUser. But how do I just call that method within the value-attribute and call a method on that return type?
Maybe it is not even possible and I missed something crucial here. But wouldn't it be really annoying to always set a property for anything you want to use instead of being able of breaking it down this way?
Thanks already for your help!
Solution
UserSession
public String getLoggedUserNickname(){
return getLoggedInUser().getNickname();
}
View
<h:outputText value="Welcome #{userSession.loggedUserNickname}" />
Or just
<h:outputText value="Welcome #{userSession.loggedInUser.nickname}" />
--
Whenever you call for property for example myProp
like userSession.myProp
it will look for getMyProp()
function. case-sensitive
OTHER TIPS
Lose the get
prefixes. This should work:
<h:outputText value="Welcome #{userSession.loggedInUser.nickname}" />