【问题标题】:Grails using inconsistent redirect uri when for the same controller同一控制器时 Grails 使用不一致的重定向 uri
【发布时间】:2012-01-30 19:10:15
【问题描述】:

我的 Grails 应用程序中有一个简单的控制器,带有简单的映射...

TCacheController {
  def index ={}
  def list= {}

}

"/tcache/" (controller: "TCache"){
   action = [GET: "index"]
}

"/tcache/items" (controller: "TCache"){
   action = [GET: "list"]
}

我所有的 URL 都是 http://.../tcache/* 的形式,一切正常。当我在这样的视图中使用<g:actionSubmit> 时出现问题...

<g:form controller="TCache">

   <g:actionSubmit class="delete" action="list" value="List Items">

提交有效,但在我的列表操作中,我有一个重定向,以防出现问题,并且该重定向导致 404,因为 Grails 发送到 /TCache/..,而不是 /tcache/...

Grails在什么情况下会改变URI的大小写,有没有办法强制它总是使用/tcache?我尝试在表单中使用 controller="tcache",但随后操作停止工作,可能是因为 Grails 找不到控制器。

【问题讨论】:

  • 列表操作中的重定向代码是什么样的?
  • list 操作的代码基本上是获取一些数据,然后执行 render[view:'list']list.gsp。如果操作中发生任何异常,我会抓住它并执行redirect(action:'index'),那时我得到404,因为它试图重定向到/TCache/...,而它应该重定向到/'tcache/...,我试图弄清楚为什么它使用大写。

标签: grails redirect groovy controller http-status-code-404


【解决方案1】:

我设法自己解决了这个问题。 Grails 对配置的约定很好,但有时 Grails 并不总是能正确猜测。

在控制器中,当渲染视图时,使用文字路径可确保 Grails 无需猜测任何内容...

TCacheController {
  def index ={}
  def list= {
     render( view: "/tcache/listitems", model:[])
}

而不是...

render( view: "listitems", model:[])

在我的例子中,有多个控制器,看起来 Grails 有点困惑,并试图在应该使用 /tcache/listitems 的情况下为完整的渲染视图路径添加前缀 /TCache/..

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-02-20
    • 2012-04-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多