代码之家  ›  专栏  ›  技术社区  ›  Blacktiger

将控制器名称映射到URL(带包)

  •  1
  • Blacktiger  · 技术社区  · 15 年前

    是否可以将grails配置为使用作为子文件夹的包来解析控制器和操作?

    /grails-app/controllers/HomeController.groovy (with action index)
    /grails-app/controllers/security/UserController.groovy (with actions index, edit)
    /grails-app/controllers/security/RoleController.groovy (with action index, edit)
    

    我希望grails自动生成以下url映射:

    http://localhost:8080/myApp/ => HomeController.index
    http://localhost:8080/myApp/security/user/ => UserController.index
    http://localhost:8080/myApp/security/user/edit => UserController.edit
    http://localhost:8080/myApp/security/role/ => RoleController.index
    http://localhost:8080/myApp/security/role/edit => RoleController.edit
    
    2 回复  |  直到 15 年前
        1
  •  3
  •   Rob Hruska MegalomanINA    15 年前

    我会对绘制它们有点谨慎 你的包裹名称。这样做将使您在将来重构变得非常困难,尤其是当您的应用程序处于野外时。这也违背了Grails的传统特性。

    但是,您仍然可以手动构造URL以映射到不同的路径。对于你的问题,这里有一个例子:

    // grails-app/conf/UrlMappings.groovy
    
    '/security/user/$action?/$id?' (controller: 'user')
    '/security/role/$action?/$id?' (controller: 'role')
    
    // the rest of the default UrlMappings are below
    '/$controller/$action?/$id?' {}
    

    基于包来实现这一点(可能使用反射,或者其他什么?),但不可取。

        2
  •  1
  •   John Engelman    15 年前
    推荐文章