Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3795532
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T13:05:53+00:00 2026-05-19T13:05:53+00:00

I need access to a specific model in my main.gsp layout. I have searched

  • 0

I need access to a specific model in my main.gsp layout. I have searched around and read that the best way to get this is to create an after filter and apply the model to the viewModel param and then access it in the gsp like so:

class MyFilters {
    all(controller:'*', action:'*') {
        after = {viewModel ->
            viewModel.client = Client.get(session.clientId)
            // println "Client is: ${client.toString()}"
        }
    }
}

And in my GSP, I should get “client” and be able to access it?

<g:if test="${client.isPartner()}">
    Do something
</g:if>
<g:else>
    Do something else
</g:else>

An example of what I’m trying to do is include GSP templates and include specific CSS stylesheets based on if the current client that is logged in belongs to a partner. The location of the special CSS and GSP templates depends on the partners name, so for example:

<g:if test="${client.isPartner()}">
    <link rel="stylesheet" type="text/css" href="/partners/${client.getPartner().toString()}/css/style.css"/>
</g:if>
<g:else>
    <link rel="stylesheet" type="text/css" href="/partners/default/css/style.css"/>
</g:else>

This would also be done with GSP templates… However, whenever I do this, I get an exception of:

ERROR grails.web.pages.GroovyPagesServlet  - Original exception : Cannot invoke method isPartner() on null object

So, apparently “client” is null in the GSP. If I uncomment out the println in the after filter shown above, it is not null at this time and the name of the client is printed. Am I doing something wrong here? Is there a better way to get at a model inside of main.gsp?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-19T13:05:54+00:00Added an answer on May 19, 2026 at 1:05 pm

    This looks like a duplicate of a question I answered recently. I suggested solving this with a tag library:

    How do I (or should I?) access the service layer from a SiteMesh template (views/layouts/main.gsp) in Grails?

    You have access to the session in a tag lib, so a closure a bit like this ought to work:

    ...
    static namespace = "yournamespace"
    ...
    def partnerDetails = { attrs, body ->
        def client = Client.get(session.clientId)
        if (client.isPartner()) {
            out << "Whatever you want to write out..."
                        // You can also call other tag libs from here - see docs
        }
        }
    ...
    

    Then you use the tag in your gsp something like this:

    <yournamespace:partnerDetails />
    

    You can also pass attributes in, which are passed into the closure through the attrs map, if that’s helpful at all.

    Finally, if you want to conditionally render the body of the tag, then you can do that like this:

    ...
    if (client.isPartner()) {
        out << body()
    }
    ....
    

    And you would call it like this:

    <yournamespace:partnerDetails>
        This is the body content that will be output in the body() call above.
    </yournamespace:partnerDetails>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two different modules that need access to a single file (One will
I have some code that I need to access both inside my models as
I need access to the uint64_t typedef from stdint.h in some wrapper code that
I need to access functionality from win32 dll , for that I am using
I am developing a Firefox extension and need to access a specific cookie from
Let's say that there are multiple users in my office that need access to
I've got an app running maximized in a borderless window and need access to
I am working on a theme for Opencart and am finding I need access
I need to access the struts.action.extension value in the struts.xml file from an interceptor.
I need to access a third-party COM server with following interface definition (idl): interface

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.