从控制器传递 gsp 参数

发布于 2024-10-10 16:06:43 字数 40 浏览 2 评论 0原文

我如何通过不是域类实例的控制器将参数传递到groovy服务器页面?

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

一花一树开 2024-10-17 16:06:43

您将参数放入返回到 GSP 的模型对象映射中,例如:

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

然后您可以通过模型映射中使用的名称来获取这些值,例如:

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

这应该显示以下内容:

My name is Maricel and my hobbies are:

 - basketball
 - photography

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
柠檬色的秋千 2024-10-17 16:06:43

最清晰的方法可能是从控制器操作返回地图:

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

现在您可以在 GSP 页面中访问该参数(默认为 myAction.gsp):

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

更多详细信息请参见此处:
http:// grails.org/doc/latest/guide/6.%20The%20Web%20Layer.html#6.1.3%20Models%20and%20Views

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

愁以何悠 2024-10-17 16:06:43

您可以这样做:

在控制器中:

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

在 gsp 页面中,您可以像这样查看名称:

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

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>
秉烛思 2024-10-17 16:06:43

您在与 gsp 同名的控制器闭包中返回参数。

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文