【问题标题】:Unable to decode the urlMapping in Grails application无法解码 Grails 应用程序中的 urlMapping
【发布时间】:2012-07-03 10:15:50
【问题描述】:

问题: 我能够执行我的控制器(ReviewMetricsController)的默认操作(listLatest)。但是,我无法通过 表单提交 显式调用其他操作(索引)。 我正在使用 Grails 2.0.4 并在安装了 Grails 插件的 Eclipse IDE 中以开发模式运行我的应用程序。

详情: 我的 gsp 中有一个表格,如下所示

    <g:form name="queryForm"
        url="[controller:'reviewMetrics', action:'index']">
        <g:actionSubmit value="submit" />
    </g:form>

当我提交上述表单时,我收到 404 错误

The requested resource (/reviewmetricsapp/reviewMetrics/index) is not available

我的控制器(reviewMetricsController.groovy)如下图所示

package reviewmetricsapp

class ReviewMetricsController {

    static scope = "session"
    static defaultAction = "listLatest"
    def gatherMetricsService
    def grailsApplication
    def latestMetrics

    def index() {
        render(view:"dashboard", model: [model: latestMetrics])
    }

    def listLatest(){
        println grailsApplication.config.metricsapp.perlScript.loc
        latestMetrics = gatherMetricsService.getLastWeekMetrics()
        println "printing ${latestMetrics}"
        render(view:"showMetrics", model: [metrics: latestMetrics])
    }

}

而我的urlMappings.groovy 如下图所示

class UrlMappings {

    static mappings = {
        "/$controller/$action?/$id?"{ constraints {
                // apply constraints here
            } }

        "/reviewMetrics/index"{
            controller="reviewMetrics"
            action="index"
        }

        "/"(view:"/index")
        "500"(view:'/error')
    }
}

感谢任何帮助

【问题讨论】:

    标签: grails grails-controller


    【解决方案1】:
    1. 您无需为您的用例更改URLMappings-文件。 (删除 reviewMetrics/index 的特殊情况处理 - 它从第一条规则处理)

    2. 请使用以下表单定义:

      <g:form name="queryForm"
         controller="reviewMetrics"
         action="index">
         [..]
      </g:form>
      
    3. 请仔细检查您的操作是否未被访问 - 只需在控制器内部输入一个普通的 index.gsp 并不要关心。我猜错误在其他地方。

    【讨论】:

    • 尝试上述方法后也没有运气!!使用 g:link 有效。我做了一个观察。当我将鼠标悬停在提交按钮上时,它会显示/reviewmetricsapp/reviewMetrics/index,但是当我将鼠标悬停在链接上时,它会显示localhost:8080/reviewmetricsapp/reviewMetrics/index。链接有效,但表单提交无效
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-11-03
    • 1970-01-01
    相关资源
    最近更新 更多