vrghost
vrghost

Reputation: 1224

How to map grails controllers to a view

Trying to do something that (I think) should be quite straight forward. In short, connected grails to a mysql database, and after a bit of fiddling around I got that working. I then created a controller which I wanted to use to extract some useful data from the database server, but don't want to send up a domain, as I don't really want to store the data in the database again.

The controller looks like this (called DbstatsController):

package dbfscalls
import groovy.swing.SwingBuilder;
import java.awt.FlowLayout

class DbstatsController {
    def userlist() {
        def query = Calls.where {}.projections { distinct 'user' }
        def userret = query.list()
        [userret:userret]
    }
    def index() {
        int numberOfRecords = Calls.count()
        [ numberOfRecords:numberOfRecords ]
        def pf = Calls.read(1)
        def firstDate = pf.CallDate
        def pl = Calls.read(numberOfRecords)
        def lastDate = pl.CallDate

    }
}

Wanted it to by default find first and last date for generating reports from database as well as number of entires in the database. I then wanted to be able to ask for a userlist to get a list of distinct users.

Next I created a view (under view/dbstats) that looks like follows:

<g:select name="userstorep"
          from="${dbstats.userlist()}"
          size="15" multiple="yes" optionKey="id"
          value="${author?.usertorep}" />

And when I call it, all that happens is that I get a error message which reads:

| Error 2014-09-16 17:24:38,030 [http-bio-8080-exec-6] ERROR errors.GrailsExceptionResolver  - NullPointerException occurred when processing request: [GET] /DBFSCalls/dbstats/index
Cannot invoke method userlist() on null object. Stacktrace follows:
Message: Error evaluating expression [dbstats.userlist()] on line [1]: Cannot invoke method userlist() on null object
    Line | Method
->>    1 | run       in /Users/bengtbjorkberg/Documents/workspace-ggts/DBFSCalls/grails-app/views/dbstats/index.gsp
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
Caused by NullPointerException: Cannot invoke method userlist() on null object
->>    1 | doCall    in Users_bengtbjorkberg_Documents_workspace_ggts_DBFSCalls_grails_app_views_dbstats_index_gsp$_run_closure2
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
|    198 | doFilter  in grails.plugin.cache.web.filter.PageFragmentCachingFilter
|     63 | doFilter  in grails.plugin.cache.web.filter.AbstractFilter
|   1142 | runWorker in java.util.concurrent.ThreadPoolExecutor
|    617 | run . . . in java.util.concurrent.ThreadPoolExecutor$Worker
^    745 | run       in java.lang.Thread

I am certain I am missing the most obvious of things, but having trawled through the information, I can't quite figure out what (bleeding obvious I am certain) part I'm missing. Would guess that it is that I have to create the object (domain controller), but don't know how.

Upvotes: 0

Views: 538

Answers (1)

cfrick
cfrick

Reputation: 37063

when rendering the view, the controller provides a model to do so - so you do not access the controller for rendering directly but you use the model provided by the controller.

in your case the index view is rendered by default and you would have to provide a model.

see http://grails.org/doc/latest/guide/theWebLayer.html#modelsAndViews

[ numberOfRecords:numberOfRecords ] in your controller looks like a noop right now. put it a the end of the index method, add your userlist there (e.g. don't return a map, but only the cound and add it as key into the map) and just use it in the select directly (no dbstats.).

Upvotes: 1

Related Questions