简体   繁体   中英

Grails: Access to model in main.gsp layout

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?

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>

That filter will only match requests that go through a controller. If you are routing directly to a view in UrlMappings.groovy, it won't get hit. Try something like this to filter urls regardless of whether they route through a controller.

class MyFilters {
    all(uri: "/**") {
        ...
    }
}

I would suggest creating following TagLib:

<g:ifPartner id="123">This client is a partner</g:ifPartner>

The corresponding code would look something like this:

def isPartner = { attrs, body ->

  def id = attrs.remove('id')
  if (Client.get(id)?.isPartner()) {
    out << body()
  }
}

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM