Search code examples
grailsgrails-2.0grails-controller

Grails, can't access logged in user instance from gsp


So here is the deal, I have a user class secUser that static hasMany = [pid:Process], also I am using spring security (not sure if it's related).

What I'm trying to do : display the drop box containing pids for the currently logged in user, had no idea how to go about it but after playing around made this

<g:if test="${secUserInstance?.pid}"> <g:select optionKey="id" optionValue="pid" name="pid" from="${secUserInstance.pid}" />

My question is: when I put this code into the main page / it does nothing, however if I go to /secUser/show/1 it shows the list of the pids belonging to the user . Can't seem to understand why it gets the info when I got to the secUser/show but doesn't anywhere else ...

P.S. I'm new to Grails and Stackoverflow but I've been stuck on this for days now and don't know where else to.


Solution

  • So your "/" path is wired to some controller in UrlMappings, in that controller there should be static defaultAction = "list" probably, which points to method that will handle your "/" path, or if no defaultAction set then it will be index(){...} method. So that method responsible for "/" path does not pass secUserInstance to your "methodName".gsp file and this is why you cannot get it. While in /secUser/show/1 you access secUserController show method wich has [secUserInstance : secUserInstance] or something similar and passes that object to gsp file. You could add secUserInstance accessible to every page using filters linke in: Add logged in user to context for all GSP pages Or just pass that user object in your method responsible fo '/'