如何更改Grails应用程序中的默认主页?

如何更改Grails应用程序中的默认主页?,grails,Grails,将Grails应用程序中的默认主页修改为不再是appName/index.gsp的配置设置是什么?当然,您可以将该页面设置为重定向,但必须有更好的方法。编辑UrlMappings.groovy 添加例如添加此规则,以使用HomeController处理根 “/”(控制器:'home')将其添加到UrlMappings.groovy中 "/" { controller = "yourController" action = "yourAction" } "/" { control

将Grails应用程序中的默认主页修改为不再是appName/index.gsp的配置设置是什么?当然,您可以将该页面设置为重定向,但必须有更好的方法。

编辑UrlMappings.groovy

添加例如添加此规则,以使用HomeController处理根


“/”(控制器:'home')

将其添加到UrlMappings.groovy中

"/" { controller = "yourController" action = "yourAction" } "/" { controller=“yourController” action=“yourAction” } 通过以这种方式配置URL映射,应用程序的主页将是yourWebApp/yourController/yourAction

(从中剪切/粘贴)

简单整洁

  • 转到文件:grails-app/conf/UrlMappings.groovy

  • 将行:“/”(视图:“/index”)替换为 “/”(控制器:'home',操作:“/index”)

  • Home是要运行的控制器(就像在SpringSecurity中可以使用“login”),action是与控制器关联的grails视图页面(在SpringSecurity中为“/auth”)

    根据您的应用程序需要添加页面重定向。

    您可以尝试以下操作
    在配置文件夹内的UrlMappings.groovy类中:

    class UrlMappings {
    
        static mappings = {
    
            "/$controller/$action?/$id?"{
                constraints {
                    // apply constraints here
                }
            }
    
            //"/"(view:"/index")
            "/" ( controller:'Item', action:'index' ) // Here i have changed the desired   action to show the desired page while running the application
            "500"(view:'/error')
        }
    }
    
    希望这有帮助,

    鲁贝尔所有的答案都是正确的! 但让我们想象一个场景:

    我将路径“/”映射到控制器:“Home”和操作“index”,因此当我访问“/app name/”时,控制器Home将被执行,但如果我键入路径“/app name/Home/index”,它仍将被执行!因此,一个资源有两条路径。它将一直工作,直到有人找到“主/索引”路径

    另一件事是,如果我有一个没有指定任何action属性的表单,那么默认情况下,它将发布到同一个控制器和action!因此,如果表单映射到“/”路径,并且没有指定操作属性,那么它将被提交到同一个控制器,但这次路径将是地址栏中的“home/index”,而不是“/”,因为它被提交到控制器/操作,而不是URI

    要解决这个问题,您必须做的是删除或注释掉这些行

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

    因此,现在当您访问“/”时,将起作用。但“主页/索引”不会。但是有一个缺陷,现在您必须通过显式写入URLMapping文件手动将所有路径映射到控制器。我想这会有帮助的

    通过以下语法使用控制器、视图和操作参数:

    class UrlMappings {
       static mappings = {
           "/" (controller:'dashboard', view: 'index', action: 'index')
           "500"(view:'/error')
       }
    }
    

    如果有人在寻找gails3.x的答案,他们会将UrlMappings.groovy移动到grails-app/controllers/appname

    正如下面的答案所说,只需将其插入以“/”开头的行即可

    就我而言,它是:

    "/"(controller:"dashboard", view:"/index")
    

    可能该方法在Grails的后续版本中有所改变,因为它不适用于1.3.7。使用daherman发布的语法对我很有效。它应该有效,但请记住使用控制器的短名称-即,如果控制器类是BooksController,则仅将其称为“books”。