我无法让 grails 控制器“渲染” 使用显式模板的方法
我刚刚开始使用 grails,但遇到了一个问题。
我有一个项目主页的“控制器”和“视图”(主页没有模型)
我将视图称为“index.gsp”,并将其放在目录views/home中
但是,无论我做什么,grails 正在尝试读取页面“home.gsp”(然后是 home.jsp),尽管我已在渲染调用中使用“template”属性显式指定了索引。
class HomeController {
String someparameter = "xyzzy"
def index = {
render(view:"home", template:"index") // I also tried "index.gsp" and "home/index.gsp"
}
}
我认为我可能错误地使用了“template”属性,因为我只看到它在无视图模板渲染的示例中使用。 然而文档并没有给出这样的限制。
有没有办法明确指定模板的名称? 我只是屈服并将其重命名为“home.gsp”,但我想了解出了什么问题。
(此应用程序中的主页没有“模型”。Grails 将使用具有模型的控制器。在此示例中,您可以通过 ${someparameter} 访问 gsp 模板中的“someparameter”。)
I'm just getting started with grails, and I'm having an issue.
I have a "controller" and "view" for the projects home page (there's no model for the home page)
I called the view "index.gsp", and put it in a directory views/home
However, no matter what I do, grails is trying to read the page "home.gsp" (and then home.jsp), despite me having explicitly specified the index with the "template" attribute in the render call.
class HomeController {
String someparameter = "xyzzy"
def index = {
render(view:"home", template:"index") // I also tried "index.gsp" and "home/index.gsp"
}
}
I think I may be using the "template" attribute wrong, because I only see it used in examples for view-less template rendering. However the documentation gives no such limitation.
Is there any way to explicitly specify the name of a template? I just caved in and renamed it "home.gsp", but I'd like to understand what's going wrong.
(The home page in this application has no "model". Grails will use the controller has the model. In this example, you can access "someparameter" in the gsp template as ${someparameter}.)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为您可能误解了 Grails 模板 是。 将模板视为可重用的片段。 模板是一个以下划线开头的 GSP,例如
_menu.gsp
,您通常可以使用等标签从另一个 GSP 中渲染它;
。同时渲染视图和模板是没有意义的。 在这一点上它们是相互排斥的。
您想要实施布局吗? 如果是这样,查看文档或grails.org 解释。
基本上,您的观点是视图的
标记中有一个
标记,这表明视图将与位于
grails-app/views/layouts/main.gsp
中的main
布局网格在一起I think you may be misunderstanding what a Grails template is. Think of a template as a reusable fragment. A template is a GSP that starts with an underscore like
_menu.gsp
that you would typically render from within another GSP with the a tag like<g:render template="menu"/>
.It doesn't make sense to render both a view and a template at the same time. They are mutually exclusive at this point.
Are you looking to implement a Layout? If so, see the docs or the grails.org explaination.
Basically, your view you would have a
<meta name="layout" content="main">
tag in the<head/>
tag of your view -- which indicates that the view will be meshed together with themain
layout located ingrails-app/views/layouts/main.gsp