Вопрос

In my GSP file

<g:form controller="hello" action="createUser">
<g:select id="users"  class="required" name="users"  from="${hello.HelloController.userList()}" noSelection="['':'-Select user-']"/>

in my HelloController

class HelloController {

        ..
        def users= []
       ...
        def userList= {

                  return users;
        }

The errors I get

groovy.lang.MissingMethodException
Message
No signature of method: static hello.HelloController.userList() is applicable for argument types: () values

I tried using just from=${userList()} and changing userList to static. All which give more errors. This used to work before I ran grails upgrade when setting up a new environment on a new computer

Это было полезно?

Решение

There are several things you should change here.

One, the preferred way of defining controller actions is as methods instead of closures.

class HelloController {
    def userList() {
        return users
    }
}

Two, your view should not be calling controller methods. Your controller action should return the model necessary for your view to render.

class HelloController {
    def list() {
        return [userList: users]
    }
}

Then in your view, which would be hello/list.gsp, you can access userList.

Другие советы

HelloController.userList()

That is trying to invoke a static method on the HelloController class and that static method does not exist. You really shouldn't be invoking controller methods from a GSP anyway. The controller should be done doing its work before the GSP is rendered. If the GSP needs a list of users you should have the controller action retrieve the users and put them in the model before the GSP is rendered. Potentially it might be the case that it makes sense to have a custom tag which you invoke from the GSP do that, but you need to have some reason to justify that. In general, having the controller put the data in the model makes more sense.

class MyController {
    def someActionWhichRendersTheViewInQuestion() {
        def users = // initialize this with a query or whatever you need
        [users: users]
    }
}

Then in your GSP...

<g:select id="users"  class="required" name="users"  from="${users}" noSelection="['':'-Select user-']"/>

I hope that helps.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top