【问题标题】:set default render file to index.html in grails 3 when routing to a webapp folder路由到 webapp 文件夹时,在 grails 3 中将默认渲染文件设置为 index.html
【发布时间】:2015-05-08 20:29:34
【问题描述】:

我在 webapp/test 文件夹中有一个 index.html 页面。如果我去路径 localhost:8080/test/index.html,页面会呈现,但如果我去 localhost:8080/test/ 它会给我页面未找到错误。实现这一目标的正确方法是什么?在我的 UrlMappings.groovy 中,我尝试了这些口味,但没有任何运气。

class UrlMappings {

    static mappings = {
        "/$controller/$action?/$id?(.$format)?" {
            constraints {
                // apply constraints here
            }
        }

        /* TRIED THESE 
        "/"(view: "/index.html")
        "/"(url: "/index.html")
         */
        "500"(view: '/error')
        "404"(view: '/notFound')
    }
}

【问题讨论】:

    标签: grails grails-3.0


    【解决方案1】:

    创建您的 test 控制器并将您的 HTML 放在 views/test/index.gsp,然后您可以使用以下命令路由到 localhost:8080/test/

    "/test" (controller: "test")
    

    如果没有上述映射指令,Grails 似乎会将 action 明确默认为 URL 中的index(与连字符问题有关:https://stackoverflow.com/a/29737608)。

    【讨论】:

    • 感谢您的更新,这些信息绝对有帮助。但是我的情况是我没有页面的支持控制器。我正在构建一个 ember 应用程序,编译后的应用程序位于 webapp 文件夹中(编译后的应用程序有 index.html)。因此,如果我编译的应用程序位于文件夹 webapp/test/ 中,那么当我的 URL 为 localhost/test 时,我希望它默认呈现 index.html,但这并没有发生,而是我必须输入完整路径localhost/test/index.html - 我希望这能进一步澄清我的问题。
    • Deewendra Shrestha:你发现了吗?我对 AngularJS 应用程序有同样的要求。我的 index.html 位于 webapp 文件夹中。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-01-17
    相关资源
    最近更新 更多