【发布时间】:2010-10-29 04:28:47
【问题描述】:
我有以下 Grails 域类:
class Product {
String name
Float basePrice
Category category
String image = "default.jpg"
static constraints = {
name(size:3..25, blank:false)
basePrice(scale:2, nullable:false)
category(inList:Category.list(), nullable:false)
image(blank:false)
}
}
我想从控制器获取图像属性的默认值(在本例中为“default.jpg”)。像这样的:
def productInstance = new Product(params)
productInstance.image = getProductPicturePath() ?: Product().image
getProductPicturePath 返回一个图像路径,但如果没有提交图像,控制器将用默认值替换空值。虽然我当然可以写这样的东西:
productInstance.image = getProductPicturePath() ?: "default.jpg"
它当然不是很干燥,我更愿意将默认值保留在一个地方。我怎样才能做到这一点?
【问题讨论】:
标签: java grails groovy controller dns