在 grails 应用程序中为索引页填充模型的正确方法是什么?默认情况下没有 IndexController,是否有其他机制可以将这个和那个列表放入模型中?
最佳答案
我不会声称这是正确的方式,但这是开始的一种方式。将 Controller 设为默认值并不需要太多。添加映射到 UrlMappings.groovy:
class UrlMappings {
static mappings = {
"/$controller/$action?/$id?"{
constraints {
// apply constraints here
}
}
"500"(view:'/error')
"/"
{
controller = "quote"
}
}
}
然后向现在的默认 Controller 添加一个索引操作:
class QuoteController {
def index = {
...
}
}
如果您要加载的内容已经是另一个操作的一部分,只需重定向:
def index = {
redirect(action: random)
}
或者要真正实现重用,请将逻辑放在服务中:
class QuoteController {
def quoteService
def index = {
redirect(action: random)
}
def random = {
def randomQuote = quoteService.getRandomQuote()
[ quote : randomQuote ]
}
}
关于grails - grails 索引页的最佳实践,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/198936/