Grails - 渲染模型和变量
通过控制器向 GSP 提供模型并提供变量的正确方法是什么?我有一个不属于模型的一部分,但也需要由 GSP 渲染。
我从什么开始:
def index = {
render controller:"test", action:"index", model:[user:user]
}
举个例子——我认为我是如何修改它的(错误的)
def index = {
def url = "http://www.stackoverflow.com"
render controller:"test", action:"index", model:[user:user, testvar: url]
}
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 controller:"test", action:"index", model:[user:user, testvar: url]
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在您的示例中,您应该能够编写如下内容:
默认情况下,Grails 视图将自动从相关操作获取变量返回。这是通过映射视图路径 - 控制器/操作名称自动完成的。
如果你调用“渲染”,那就有点不同了。您可以渲染任何可能与您的控制器/操作无关的视图。我建议仅在需要时使用它。
In your example, you should be able to write things like this:
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.
你的代码看起来是正确的。这会将模型中的 var 发送到 gsp,但如果您想像在
remoteFunction
中那样将其发送到 javascript,则需要使用grails.converters.JSON
如下: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 usegrails.converters.JSON
as next: