Grails索引页的最佳做法页面
答
我不会说这是正确的方式,但它是一种开始的方法。将控制器作为默认值并不需要太多。添加映射UrlMappings.groovy:
class UrlMappings {
static mappings = {
"/$controller/$action?/$id?"{
constraints {
// apply constraints here
}
}
"500"(view:'/error')
"/"
{
controller = "quote"
}
}
}
然后添加一个索引行动,现在默认控制器:
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 ]
}
}
答
好的答案:如果您需要填充索引页的模型,现在是时候从使用直接index.gsp改变为索引控制器。
邪恶的回答:如果你创建一个控制器是'*'的过滤器,即使对于静态页面,它也会被执行。
答
我无法让Ed T上面的例子工作。从那时起,Grails可能已经改变了?
一些试验后,并在网络上的一些翻找,我结束了这UrlMappings.groovy
:
"/"(controller: 'home', action: 'index')
我的HomeController看起来是这样的:
class HomeController {
def index = {
def quotes = = latest(Quote.list(), 5)
["quotes": quotes, "totalQuotes": Quote.count()]
}
}
而在,我有一个index.gsp
文件。这使得视图中的index.gsp
文件变得不必要,所以我将其删除。
答
Grails的1.3.6只是增加
"/index.gsp"(uri:"/")
到UrlMappings.groovy为我工作得很好。它与添加如前所述的新控制器和映射具有相同的效果。
下面是我的完整UrlMappings.groovy:
class UrlMappings {
static mappings = {
"/$controller/$action?/$id?"{
constraints {
// apply constraints here
}
}
"/"(view:"/index")
"500"(view:'/error')
"/index.gsp"(uri:"/")
}
}
我认为UrlMappings也许应该已经阅读` “/”{控制器: “引用”}`。直到我将“=”更改为“:”,它才起作用。 – 2012-07-30 17:50:30
`static defaultAction =“random”` - 而不是从索引操作重定向,您可以将defaultAction静态字段添加到控制器,并将期望的操作名称作为值。 – topr 2013-02-23 00:33:28