8

i am new to grails and i want to use a method from a specific controller in my index.gsp

In Index.gsp i tried

<g:each in="${MyController.myList}" var="c">
     <p>${c.name}</p>
</g:each>

but it says that the property is not available.

MyController contains a property like:

   def myList = {
       return [My.findAll()  ]
   }

What am i doing wrong? Is there a good tutorial about the communication between the grails-parts?

Or is there a better way to get information printed via gsp?

Thanks

elCapitano
  • 1,821
  • 3
  • 22
  • 42
  • I think it would be verry helpfull if you can tell me, where i can find a best practice Grails/Groovy project. – elCapitano Nov 25 '10 at 16:38

1 Answers1

19

Generally, when using the Model-View-Controller pattern, you don't want your view to know anything about controllers. It's the controller's job is to give the model to the view. So instead of having index.gsp respond to the request directly, you should have a controller handle it. The controller can then get all the domain objects necessary (the model), and pass them on to the view. Example:

// UrlMappings.groovy
class UrlMappings {
    static mappings = {
        "/$controller/$action?/$id?"{
            constraints {
                // apply constraints here
            }
        }

        "/"(controller:"index") // instead of linking the root to (view:"/index")
        "500"(view:'/error')
    }
}

// IndexController.groovy
class IndexController {
    def index() {  // index is the default action for any controller
        [myDomainObjList: My.findAll()] // the model available to the view
    }
}

// index.gsp
<g:each in="${myDomainObjList}" var="c">
    <p>${c.name}</p>
</g:each>
grantmcconnaughey
  • 10,130
  • 10
  • 37
  • 66
ataylor
  • 64,891
  • 24
  • 161
  • 189
  • 1
    That's way too verbose. If you're rendering index.gsp from the 'index' action it's not necessary to specify the view attribute and model attribute, just return the model map. – Burt Beckwith Nov 25 '10 at 18:25
  • Ok, I've updated the controller to let the view be chosen by convention rather than calling render explicitly. – ataylor Nov 25 '10 at 18:31
  • So you have to use a controller for each .gsp site if you want to get model objects? – elCapitano Nov 26 '10 at 07:49
  • 1
    You don't have to; you can import any class and use it directly in your gsps, but using a controller is the norm. – ataylor Nov 26 '10 at 16:46
  • @BurtBeckwith How can I render localhost:8080/index.gsp and use index action of AppsController? Usually URLmapping.groovy redirects me to localhost:8080/apps/index.gsp – Sap Sep 25 '12 at 21:33
  • @Grrrrr ask a new question - it's not good for comments to become discussion threads – Burt Beckwith Sep 25 '12 at 23:12
  • I modified the above urlmapping since the static mapping should remain the default "/$controller/$action?/$id?". In this case we only need to change url mapping for the root to use the index controller – Arye Rosenstein Apr 02 '14 at 19:27