Results 1 to 6 of 6

Thread: How Can I Pass Variables from a Controller to a GSP aprt from model object?

  1. #1

    Thumbs down How Can I Pass Variables from a Controller to a GSP aprt from model object?

    Hi,

    I have gone through some grails article and find out the following way to pass a variable to GSP from the Controller:

    As per the Article:

    If your action doesn’t explicitly return a model, all the controller’s properties will be
    available inside the view. Remember that a controller is created for each request, so this
    approach is thread- safe. Here is an example:

    class MainController {
    String message; def index() {
    message="Welcome to the Grrovy&Grails Forum"
    }
    }

    The variable message is now accessible inside grails-app\views\main\index.gsp as
    follows:
    <html>
    <head><title>Main</title></head>
    <body> ${message}</body>
    </html>

    But it's not working, I am using Grails 2.0.4.

    Kindly confirm me that can I access the Controller properties in the controller respective GSP files or not?

    Thanks in advance.

    Regards,
    Baji

  2. #2
    Join Date
    Jul 2007
    Posts
    124

    Default

    This works for me. As always, when you see unexpected behavior try running 'grails clean' and re-running.

    But I think this is a bad approach. It's inconsistent; class fields aren't added to the model if you do return one. It's far better to not use any state fields (pretend the class is a singleton) and add extra data to the model using an afterInterceptor or a filter.

  3. #3

    Unhappy

    Quote Originally Posted by burtbeckwith View Post
    This works for me. As always, when you see unexpected behavior try running 'grails clean' and re-running.

    But I think this is a bad approach. It's inconsistent; class fields aren't added to the model if you do return one. It's far better to not use any state fields (pretend the class is a singleton) and add extra data to the model using an afterInterceptor or a filter.
    Hi,

    Thanks a lot for your clarification

    Regards,
    Baji

  4. #4
    Join Date
    Jul 2011
    Location
    Wokingham UK
    Posts
    35

    Default

    All the doc's I read say that the Controller passes a MAP to the view. So I would have expected to see the extra line

    [message: message]

    after 'message= ...' i.e. between the assignment and the closing "}" of the method.

  5. #5
    Join Date
    Jul 2007
    Posts
    124

    Default

    That's just one way to do it. You can also render a response directly (e.g. JSON or XML) or redirect. There's no one way to get data to the view.

    But the approach mentioned by the OP is an old one, inspired by the Rails approach where the controller is the model. It requires a stateful controller where a new instance is created for each request, and is rarely used as far as I can tell. At some point we'll probably drop support for this and make controllers proper singletons.

  6. #6
    Join Date
    Sep 2012
    Posts
    2

    Default

    Quote Originally Posted by burtbeckwith View Post
    This works for me. As always, when you see unexpected behavior try running 'grails clean' and re-running.

    But I think this is a bad approach. It's inconsistent; class fields aren't added to the model if you do return one. It's far better to not use any state fields (pretend the class is a singleton) and add extra data to the model using an afterInterceptor or a filter.
    This can be very frustrating for newbies like me. I struggled like crazy to make this work. I am using grails 2.0.4 as well. Even clean did not help. I had to restart STS for it to happen. I am writing this here so that no other poor soul has to go through the ordeal I just went through.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •