在grails应用程序中填充索引页模型的正确方法是什么?默认情况下没有IndexController,有没有其他一些机制可以将这个和那个列表放入模型中?Grails索引页的最佳做法页面
27
A
回答
36
我不会说这是正确的方式,但它是一种开始的方法。将控制器作为默认值并不需要太多。添加映射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 ]
}
}
4
好的答案:如果您需要填充索引页的模型,现在是时候从使用直接index.gsp改变为索引控制器。
邪恶的回答:如果你创建一个控制器是'*'的过滤器,即使对于静态页面,它也会被执行。
19
我无法让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
文件变得不必要,所以我将其删除。
0
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:"/")
}
}
相关问题
- 1. Django索引页最佳/最常见的做法
- 2. 登录页面的最佳做法?
- 3. 创建完整页面放大页面的最佳做法?
- 4. jsp页面布局的最佳做法是什么?
- 5. 处理辅助页面请求的最佳做法
- 6. 在rails应用程序中静态页面的最佳做法
- 7. 用重定向POST'ing到新页面的最佳做法?
- 8. 返回到发起的回传页面。最佳做法
- 9. 在某些WordPress页面上包含JavaScript的最佳做法
- 10. 编码用户页面的最佳做法是什么?
- 11. 应用登录页面的最佳做法?
- 12. 确认页面的最佳方法
- 13. 在.NET页面中进行分页的最佳方法
- 14. 将索引页面重定向到索引页面,并在htaccess中附加了索引页面
- 15. UIPageViewController无法跟踪页面索引
- 16. Codeigniter,安全搜索的最佳做法
- 17. 最好的方法来页面与页面的asp.net-mvc页面
- 18. 为YII中的特定页面移除页眉和页脚的最佳方法
- 19. 使用WHERE/LIKE时索引/全文的最佳做法
- 20. 索引唯一文本字段的最佳做法是什么?
- 21. 无法打开新页面预计在WordPress的索引页
- 22. 缓慢activeadmin索引页面
- 23. WordPress静态索引页面
- 24. 页面索引控件
- 25. jQuery搜索引擎/页面
- 26. CodeIgniter更改页面索引
- 27. 有Harp.js和GitHub页面的文件夹结构的最佳做法吗?
- 28. 最佳做法:喜欢网站的按钮或网站的Facebook页面?
- 29. Grails - 如何定义起始索引页面URL
- 30. Grails在其他页面渲染页面
我认为UrlMappings也许应该已经阅读` “/”{控制器: “引用”}`。直到我将“=”更改为“:”,它才起作用。 – 2012-07-30 17:50:30
`static defaultAction =“random”` - 而不是从索引操作重定向,您可以将defaultAction静态字段添加到控制器,并将期望的操作名称作为值。 – topr 2013-02-23 00:33:28