我无法让 grails 控制器“渲染” 使用显式模板的方法

发布于 2024-07-30 17:57:07 字数 656 浏览 7 评论 0原文

我刚刚开始使用 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 技术交流群。

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

发布评论

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

评论(1

抱猫软卧 2024-08-06 17:57:07

我认为您可能误解了 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 the main layout located in grails-app/views/layouts/main.gsp

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