grails - grails 索引页的最佳实践

标签 grails indexing model

在 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/

相关文章:

python - 如何在优雅地保存在 Django 1.5 之前使用 full_clean() 进行数据验证?

asp.net-mvc - 使用 RedirectToAction 时 View 上的模型为空

grails - 如何将类传递给 Groovy 的 Eval 绑定(bind)?

grails - Grails Map域类的列名问题

firebase - Firestore 单字段索引豁免限制

MongoDb 索引全文搜索

php - 是否可以创建一个不引用数据库表的 CakePHP 模型?

grails - Grails分页中的javascript

performance - Grails文件上传:Tomcat内存耗尽

mysql - 优化 Mysql UNION 查询以加快加载时间