Problem with if/else

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

Problem with if/else

DrunkeN
Hi,

I have a problem with the if/else.

First the code:

            Username: ${session.user}<br />
           
            <g:if test="${session.user  == 'mike'}">
                             Hello my god!
                        </g:if>
                        <g:else>
                             Hello ${session.user}! How are you?
                        </g:else>

So when the user is mike it should write Hello my god. When I login with the username "mike" the output is as follows:

Username: mike
Hello mike! How are you?

what's wrong here? Why he is jumping to else and doesn't go into if?
Reply | Threaded
Open this post in threaded view
|

Re: Problem with if/else

houbie
session.user is an object that can print as 'mike', but is not equal to the String 'mike'
you probably need something like <g:if test="${session.user?.name  == 'mike'}">

Grtz,
Ivo
---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email


Reply | Threaded
Open this post in threaded view
|

Re: Problem with if/else

DrunkeN
Hi, thank you that works.

Can you tell me what the "?" is doing? Because i tried myself before i create this topic session.user.name :D

Many thanks! ;)
Reply | Threaded
Open this post in threaded view
|

Re: Problem with if/else

Vladimir Moroz
"?" is used to prevent NullPointerException if session.user is null. 


On Mon, Dec 17, 2012 at 1:02 PM, DrunkeN <[hidden email]> wrote:
Hi, thank you that works.

Can you tell me what the "?" is doing? Because i tried myself before i
create this topic session.user.name :D

Many thanks! ;)



--
View this message in context: http://grails.1312388.n4.nabble.com/Problem-with-if-else-tp4639145p4639147.html
Sent from the Grails - user mailing list archive at Nabble.com.

---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email