如何限制 grails 中域属性的可见性? [英] How to restrict visibility of domain properties in grails?
本文介绍了如何限制 grails 中域属性的可见性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
是否有任何推荐的方法来限制 grails 中域的可见性?
Is there any recommended way to restrict the visibility of a domain in grails?
通常你会做一些事情,比如获取一些供外部使用的界面:
Normally you you do something like to get some interface for external use:
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
但默认情况下,不应填充域中的属性.所以我有话要说
But by default there proerties in a domain that should not be populated. So I need something to say
SELECT id, name, foo1, foo2 FROM product
因此答案中仅包含属性列表.
so only a list of properties is included in the answer.
推荐答案
您可以使用类似于视图的第二个域类.诀窍是配置映射,使其与 Product 类具有相同的表:
You can use a second domain class sort of like a view. The trick is to configure the mapping so it has the same table as the Product class:
class ProductView {
String name
Foo foo1
Foo foo2
static mapping = {
table 'product'
}
}
然后在您的用户界面中使用它:
Then use that in your UI:
def productList = ProductView.list()
withFormat {
html {[productList:productList]}
json { render productList as JSON }
xml { render productList as XML }
rss { render(feedType:"rss", productList)}
}
这篇关于如何限制 grails 中域属性的可见性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文