gsp parameter passing from controller

◇◆丶佛笑我妖孽 提交于 2019-12-03 06:04:58

问题


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


回答1:


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



回答2:


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




回答3:


You can do it like this :

In the controller:

def myaction = {
    String name = "Tony Danza"
    [name: name]
}

In the gsp page you can view the name like so:

<body>
    My name is ${name}
</body>



回答4:


You return the parameters in the closure of the controller that has the same name as the gsp.



来源:https://stackoverflow.com/questions/4624214/gsp-parameter-passing-from-controller

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!