Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

gsp parameter passing from controller

Tags:

grails

groovy

gsp

how can i pass parameters to a groovy server page via a controller that are not an instance of a domain class ?

like image 590
SomeEUGuy Avatar asked Jan 07 '11 09:01

SomeEUGuy


2 Answers

You put your parameters into the model object map returned to your GSP, for example:

def index = { def hobbies = ["basketball", "photography"] 
render(view: "index", model: [name: "Maricel", hobbies: hobbies]) }

Then you get those values accessing them by the name you use in your model map, for example:

My name is ${name} and my hobbies are:
<ul>
<g:each in="${hobbies}" var="hobby">
<li>${hobby}</li>
</g:each>
</ul>

That should display the following:

My name is Maricel and my hobbies are:

 - basketball
 - photography
like image 51
Maricel Avatar answered Nov 12 '22 15:11

Maricel


The clearest way is probably to return a map from your controller action:

...
def myAction = {
    [myGreeting: "Hello there, squire!"]
}
...

Now you can access that parameter in your GSP page (by default myAction.gsp):

...
<p><%= myGreeting %></p>
...

More details here: http://grails.org/doc/latest/guide/6.%20The%20Web%20Layer.html#6.1.3%20Models%20and%20Views

like image 8
Martin Dow Avatar answered Nov 12 '22 16:11

Martin Dow