开发者

Grails - rendering a model and a variable

What is the correct way to give a model to a GSP via the controller but also supply a variable. I have one that is not part of the model, but also needs to be rendered by the GSP.

What I started with:

def index = {
      render controller:"test", action:"index", model:[user:user]  
}

For an example - how I modified it (incorrectly) I think

def index = {
      def url = "http://www.stackoverflow.com"
      render contro开发者_Python百科ller:"test", action:"index", model:[user:user, testvar: url]  
}


In your example, you should be able to write things like this:

def index = {
      def url = "http://www.stackoverflow.com"
      return [user:user, testvar: url]  
}

By default, Grails view will automatically get the variable returns from the relevant action. This is done automatically by mapping view path - controller/action name.

If you call "render", it's a bit different. You can render any view which may not associated to your controller/action. I recommend only using it when in need.


Your code looks right. That will send the var in the model to the gsp, but if you want to send it to the javascript as in a remoteFunction, you need to use grails.converters.JSON as next:

render [var1:value1, var2,value2, ...] as JSON
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜