首页 > 解决方案 > 如何将grails 3中的动作映射到“/”?

问题描述

我正在使用 Grails 3.3.10 并尝试将“/”映射到控制器操作,用户在成功登录后被重定向。

在 UrlMappings 我有:

class UrlMappings {

   static mappings = {

      "/"(controller:'app')
   ...
...

当用户登录时,应用程序被重定向到根http://localhost:8090/,但它显示了从 Grails 生成的视图:

Welcome to Grails

Congratulations, you have successfully started your first Grails
application! At the moment this is the default page, feel free to
modify it to either redirect to a controller or display whatever
content you may choose. Below is a list of controllers that are
currently deployed in this application, click on each to execute
its default action:

Available Controllers:

....

我删除了默认的 index.gsp 和 main.gsp 布局,但是带有控制器的视图仍然出现,我无法执行我的操作。

如果我删除 UrlMapping "/"(controller:'app'),则操作执行正常并且视图是正确的,但 URL 是http://localhost:8090/app/index

是否可以显示来自 app/index 的视图是映射到“/”的 URL?

标签: grailsgrails3

解决方案


是否可以显示来自 app/index 的视图是映射到“/”的 URL?

是的。请参阅https://github.com/jeffbrown/pablopazosurlmapping上的项目。

https://github.com/jeffbrown/pablopazosurlmapping/blob/452980ca99bbd5ccc217047534798001a8d7d9cb/grails-app/controllers/pablopazosurlmapping/UrlMappings.groovy

package pablopazosurlmapping

class UrlMappings {

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

        "/"(controller:'app')
        "500"(view:'/error')
        "404"(view:'/notFound')
    }
}

https://github.com/jeffbrown/pablopazosurlmapping/blob/452980ca99bbd5ccc217047534798001a8d7d9cb/grails-app/controllers/pablopazosurlmapping/AppController.groovy

package pablopazosurlmapping

class AppController {
    def index() {
        [name: 'Pablo']
    }
}

https://github.com/jeffbrown/pablopazosurlmapping/blob/452980ca99bbd5ccc217047534798001a8d7d9cb/grails-app/views/app/index.gsp

<%@ page contentType="text/html;charset=UTF-8" %>
<html>
<head>
    <title>Demo</title>
</head>

<body>
<h2>${name} Was Here!</h2>
</body>
</html>

我希望这会有所帮助。


推荐阅读