How to restrict visibility of domain properties in grails?

随声附和 提交于 2019-12-21 03:15:14

问题


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)}
}

which is equal to

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.


回答1:


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)}
}


来源:https://stackoverflow.com/questions/2789176/how-to-restrict-visibility-of-domain-properties-in-grails

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!