gpt4 book ai didi

grails - Grails Controller 映射,完全误解

转载 作者:行者123 更新时间:2023-12-02 14:22:34 24 4
gpt4 key购买 nike

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

这是我的代码:

package naturalselector

class UrlMappings {

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

Controller 类:
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")
}
}

Controller 位于grails-app \ controllers \ naturalselector \中
网址映射位于同一目录中。

在所有示例中, Controller 均具有小写值。
我不明白是包吗?为什么将 Controller 指定为软件包?
我只想在 Controller 中执行方法,我还不想渲染任何页面,只需重定向回索引即可。谢谢。

最佳答案

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')
}

关于grails - Grails Controller 映射,完全误解,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37231809/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com