grails - 如何使用grails中目标 Controller 的参数重定向到不同 Controller 的 Action

标签 grails

我有两个域类 Author 和 Book 相关的一对多。当我为特定作者创建一本新书时,我想(在单击保存按钮后)重定向到作者 Controller 的显示操作,以便显示特定作者的全部详细信息以及他创作的所有书籍的列表。

class Author {
  String fistName
  String secondName

  static hasMany = [books: Book]
}

class Book {
  String title
  String dateOfPublication
  //Author author

  static belongsTo = [author: Author]
}


//BookController
def save() {
    def bookInstance = new Book(params)
    if (!bookInstance.save(flush: true)) {
        render(view: "create", model: [bookInstance: bookInstance])
        return
    }

    flash.message = message(code: 'default.created.message', args: [
      message(code: 'book.label', default: 'Book'),
      bookInstance.id])

    if(params.tempid){
      redirect(action: "show", controller: "author", id: params.id)
    }else{
      redirect(action: "show", id: params.tempid)
    }
}

然而,上面的代码并没有按照我的意愿重定向我,而是执行了 else 语句,并显示了 flash 消息“未找到 ID 为 null 的书”

下面是在 Author 类的 show.gsp 中呈现的 _book.gsp 模板。
 <table class="table table-striped table-hover" id="book-data">
        <thead>
          <th class="sortable">Title</th>
          <th class="sortable">Date of Publication</th>
        </thead>

        <tbody>
          <g:each in="${authorInstance.book}" status="i" var="p">
            <tr class="prop">
              <td style="vertical-align: middle;" valign="top" class="value">${p?.title}</td>
              <td style="vertical-align: middle;" valign="top" class="value">${p?.dateOfPublication}</td>
             <td style="vertical-align: middle;" valign="top"><g:link class="btn btn-edit" action="edit" controller="book" id="${p?.id}"> Edit</g:link></td>
            </tr>
          </g:each>
        </tbody>
      </table>

我怎样才能做到这一点。?

感谢帮助。

编辑:authorInstance.id 更改为 params.id

最佳答案

如果您想在保存 book 后重定向到您的作者实例,你应该发送作者的id在保存请求参数中。默认情况下,作者 ID 包含在 params 中映射为 author.id .
所以这应该工作:redirect(action: "show", controller: "author", id: params.author.id)

关于grails - 如何使用grails中目标 Controller 的参数重定向到不同 Controller 的 Action ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41339617/

相关文章:

grails - 如何在 GORM 中建立可选的一对一关系?

mysql - 找不到适用于 jdbc :mysql://localhost 的合适驱动程序

postgresql - 域验证但不会保存

eclipse - 将 Grails 项目从 2.2.1 升级到 2.3.6;使用 Tomcat 运行应用程序失败,无法使用 Jetty 进行调试

hibernate - 为什么域类静态方法不能从 grails "service"内部工作?

java - Grails 1.3.7 执行脚本时出错 War : Provider for javax. xml.parsers.SAXParserFactory 找不到

grails - grails当前页面亮点

unit-testing - PageRenderer 在单元测试期间返回空对象

grails - Grails repo 失败了吗?

hibernate - 自定义嵌入字段的列名