【问题标题】:Getting data into a GSP将数据导入 GSP
【发布时间】:2011-03-20 04:10:52
【问题描述】:

我有一个在另一个 GSP 中呈现的模板。我似乎找不到将数据填充到其中的方法。这是我的学生班级/控制器的生成显示页面。所以学生控制器应该处理“显示”功能,但它永远不会进入该功能。 有没有办法从学生表中获取数据并填充 studentInstance.id、studentInstance.Name 等?

此外,是否有任何真正解释 GSP 的好书/网站。我找不到任何对更复杂的动作有很好的参考。

谢谢

【问题讨论】:

    标签: grails gsp


    【解决方案1】:

    您可以在Grails user guide 中找到您需要的有关控制器、GSP 和模板的所有信息,更具体地说,在 Web 层部分。

    【讨论】:

    • 我已经阅读了用户指南。该页面位于访问控制器的正确文件夹中,但是,永远不会调用控制器来提取数据。我还尝试直接从浏览器加载嵌入式渲染调用之外的页面,并且再次没有调用控制器中的 show 函数。
    • 马克你能分享一些你的代码吗?请在 UrlMappings.groovy 中包含您如何映射您的操作
    • 我的一个类是一个账户:class Account { Double accountBalance Integer accountNumber Boolean active static belongsTo = [customer: Customer] static hasMany = [bill: Bill, payment: Payment, fee: Fees, credit : Credit] static mappedBy = [bill: "billingAccount", payment: "paidAccount", fee: "chargedAccount", credit: "creditedAccount"] 静态约束 = {accountNumber(nullable: true) accountBalance(nullable: true) bill(nullable : true) credit(nullable: true) fee(nullable: true)} String toString(){return accountNumber}}
    • URLMapping 是:"/account/_showAccount.gsp"{ controller="account" action="show"} 现在的 _showAccount.gsp 只是显示页面的生成视图版本,生成。所以我没有做任何事情来真正改变控制器中显示功能的链接。它位于视图的 /account 目录中。
    • 账户控制器 show 是默认生成的: def show = { def accountInstance = Account.get(params.id) println "SHOW Params: ${params}"
    【解决方案2】:

    要将数据传递到模板中,请使用model 参数。这是我从我的一些代码中提取的一个示例:

    <g:render template="/voteAndScore" model="['wysiap': wysiap, 'showVote': false, 'showDiscuss': true, 'updatedWysiaps': updatedWysiaps]" />
    

    至于文档,几乎所有东西都可用here。但我发现 Grails in Action 也是一本有用的读物​​。

    【讨论】:

    • Kaleb,感谢您的帖子,我正在渲染,但是是动态的,并且数据没有被提取。它现在正在工作。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-06-01
    • 1970-01-01
    • 1970-01-01
    • 2016-10-04
    • 2013-03-20
    • 1970-01-01
    相关资源
    最近更新 更多