Search code examples
grailsmodel-view-controllerseparation-of-concerns

One View and Multiple Domains/Controllers in Grails


I have 2 domains: user & item + corresponding controllers. There is no link (domains don't have a relationship) between user & item. I need to create a view that displays information from both user & item. For example list all users and items in a single view. How can I achieve that? What would be the right approach?

Thank you.


Solution

  • Grails uses a convention but that convention can easily be broken.

    class SomeController {
    
       def list() {
          def users = Users.list()
          def items = Item.list()
          [users: users, items: items]
       }
    }
    

    grails-app/views/some/list.gsp

    <ul>
      <g:each in="${users}">
        <li>${it.firstName}</li>
      </g:each>
    </ul>
    
    <ul>
      <g:each in="${items}">
        <li>${it.name}</li>
      </g:each>
    </ul>