有没有建议的方法来限制域的可见性?
通常,您会做一些事情以获取一些供外部使用的界面:
def productList = Product.list()
withFormat {
html {[productList:productList]}
json { render productList as JSON }
xml { render productList as XML }
rss { render(feedType:"rss", productList)}
}
等于
SELECT * FROM product
但是默认情况下,不应填充域中的属性。所以我有话要说
SELECT id, name, foo1, foo2 FROM product
因此答案中仅包含属性列表。
最佳答案
您可以使用类似于 View 的第二个域类。诀窍是配置映射,使其具有与Product类相同的表:
class ProductView {
String name
Foo foo1
Foo foo2
static mapping = {
table 'product'
}
}
然后在您的UI中使用它:
def productList = ProductView.list()
withFormat {
html {[productList:productList]}
json { render productList as JSON }
xml { render productList as XML }
rss { render(feedType:"rss", productList)}
}
关于api - 如何限制grails中域属性的可见性?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2789176/