Grails 控制器映射,完全误解

Grails Controller Mapping, Complete misunderstanding

好的,我正在尝试学习 grails,但我不明白 UrlMappings 是如何工作的。

这是我的代码:

package naturalselector

class UrlMappings {

static mappings = {
    "/pleasemapit"(view: '/index')
    "/creatures/" {
      controller = 'NaturalSelectionController'
      action = 'viewCreatures'
    }
    "500"(view:'/error')
    "404"(view:'/notFound')
}
}

控制器class:

package naturalselector

class NaturalSelectionController {

def viewCreatures() {
  println("HIT viewCreatures")
  List creatures
  6.times { idx ->
    creatures.add(new RandomCreature())
    println creatures.get(idx)
  }
  redirect (view: "/index")
}
}

控制器在 grails-app\controllers\naturalselector\ UrlMappings 在同一个目录中。

在所有示例中,控制器都具有小写值。 我不明白。是包裹吗?为什么要将控制器指定为包? 我只想在控制器中执行方法,我还不想渲染任何页面,只是重定向回 index.html 。谢谢。

Is it a package?

没有

Why would you specify controller as a package?

你不会。

而不是这个...

static mappings = {
    "/pleasemapit"(view: '/index')
    "/creatures/" {
        controller = 'NaturalSelectionController'
        action = 'viewCreatures'
    }
    "500"(view:'/error')
    "404"(view:'/notFound')
}

使用这个...

static mappings = {
    "/pleasemapit"(view: '/index')
    "/creatures" {
        controller = 'naturalSelection'
        action = 'viewCreatures'
    }
    "500"(view:'/error')
    "404"(view:'/notFound')
}

或者这个...

static mappings = {
    "/pleasemapit"(view: '/index')
    "/creatures"(controller: 'naturalSelection', action: 'viewCreatures')
    "500"(view:'/error')
    "404"(view:'/notFound')
}