如何将grails 3中的动作映射到“ /”?

问题描述 投票:0回答:1

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

在我的网址映射中:

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“ /”(控制器:'app'),则执行该操作,并且视图是正确的,但URL为http://localhost:8090/app/index

是否可以将应用程序/索引中的视图显示为映射到“ /”的URL?

grails grails3
1个回答
0
投票

是否有可能显示来自应用程序/索引的视图,该视图是映射的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>

我希望有帮助。

© www.soinside.com 2019 - 2024. All rights reserved.