Grails: use controller from index.gsp

13,495

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>
Share:
13,495
elCapitano
Author by

elCapitano

Computerscience Informationscience Philosophy

Updated on June 26, 2022

Comments

  • elCapitano
    elCapitano almost 2 years

    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

  • Burt Beckwith
    Burt Beckwith over 13 years
    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.
  • ataylor
    ataylor over 13 years
    Ok, I've updated the controller to let the view be chosen by convention rather than calling render explicitly.
  • elCapitano
    elCapitano over 13 years
    So you have to use a controller for each .gsp site if you want to get model objects?
  • ataylor
    ataylor over 13 years
    You don't have to; you can import any class and use it directly in your gsps, but using a controller is the norm.
  • Sap
    Sap over 11 years
    @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
  • Burt Beckwith
    Burt Beckwith over 11 years
    @Grrrrr ask a new question - it's not good for comments to become discussion threads
  • Arye Rosenstein
    Arye Rosenstein about 10 years
    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