我在cotrolller中有以下定义来获得平均价格
def productAvg = Product.executeQuery("select avg(price) from Product")
Product productavg = new Product (price: productAvg)
[productavg:productavg]
现在在视野中
<li class="fieldcontain">
<span id="productavg-label" class="property-label"><g:message code="productavg.price.label" default="Avg. Price" /></span>
<span class="property-value" aria-labelledby="productavg-label"><g:fieldValue bean="${productavg}" field="price"/></span>
</li>
当我转到页面时,我看到了
任何人都可以告诉我为什么额外的[]以及我如何删除它?
答案 0 :(得分:0)
Product.executeQuery("select avg(price) from Product")
(或实际select avg(price) from Product
)返回列表,包含一个元素。这就是你所看到的,1元素阵列。
您需要获取此列表的第一个元素。
试试这个:
def productAvg = Product.executeQuery("select avg(price) from Product").first()
Product productavg = new Product (price: productAvg)
PS还会检查它是否为非空,可以在新的空DB上猜测