Announcement Announcement Module
Collapse
No announcement yet.
How Can I Pass Variables from a Controller to a GSP aprt from model object? Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • 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
    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.

    Comment


    • #3
      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

      Comment


      • #4
        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.

        Comment


        • #5
          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.

          Comment


          • #6
            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.

            Comment

            Working...
            X