gpt4 book ai didi

Grails Controller ,命令对象爆炸

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

在 Grails Controller Action 中,为了验证,我们使用命令对象。问题是 CommandObject 类的数量激增。

 def publish = { PublishCommand command ->
if (command.hasErrors()) {
return redirect(action: 'errors',params:params)
}
//rest of the code
}
......

Class PublishCommand {
long personId
String name
static constraints = {
personId(nullable: false, blank: false)
name(nullable: false, blank: false)

}
}

PublishCommand 类仅用于此数据绑定(bind)和验证目的。此类类的数量激增,为应用程序的每个操作创建了 1 个。
问题是,有没有办法可以让这个 PublishCommand 作为内部类?或者我不必创建这么多类的其他方式?

最佳答案

将命令对象类放在与 Controller 相同的 .groovy 文件中(在 Controller 类之后)是一种非常常见的做法。这将有助于减少您必须管理的文件数量。否则,您将遵循最佳实践(根据您的描述)。

关于Grails Controller ,命令对象爆炸,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7590506/

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