I have the following Grails domain class:
我有以下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)
}
}
From a controller, I want to get the default value for the image property (in this case "default.jpg"). Something like this:
从控制器,我想获得image属性的默认值(在本例中为“default.jpg”)。像这样的东西:
def productInstance = new Product(params)
productInstance.image = getProductPicturePath() ?: Product().image
getProductPicturePath returns an image path, but in case no image was submitted, the controller shall replace the null value with the default. While I could certainly write something like this:
getProductPicturePath返回图像路径,但是如果没有提交图像,控制器应使用默认值替换空值。虽然我当然可以这样写:
productInstance.image = getProductPicturePath() ?: "default.jpg"
It's certainly not very DRY, and I would prefer to keep that default value in one place. How can I achieve this?
它肯定不是很干,我宁愿将默认值保存在一个地方。我怎样才能做到这一点?
2 个解决方案
#1
The obvious solution is to declare it as a constant and then refer to it.
显而易见的解决方案是将其声明为常量然后引用它。
class Product {
static DEFAULT_IMAGE = "default.jpg"
String name
Float basePrice
Category category
String image = DEFAULT_IMAGE
static constraints = {
name(size:3..25, blank:false)
basePrice(scale:2, nullable:false)
category(inList:Category.list(), nullable:false)
image(blank:false)
}
}
productInstance.image = getProductPicturePath() ?: Product.DEFAULT_IMAGE
Even better, make getProductPicturePath()
return the default value.
更好的是,make getProductPicturePath()返回默认值。
#2
If there is no image
key in params
如果params中没有图像键
def productInstance = new Product(params)
will skip this property and leave the default value defined in the domain class. So, assuming that your params doesn't contain an image
key, the simplest way would be to skip the assignment of the image
property at all for those products that have no image:
将跳过此属性并保留域类中定义的默认值。因此,假设您的参数不包含图像密钥,最简单的方法是跳过为没有图像的产品分配图像属性:
def imagePath = getProductPicturePath()
if(imagePath) {
productInstance.image = imagePath
}
#1
The obvious solution is to declare it as a constant and then refer to it.
显而易见的解决方案是将其声明为常量然后引用它。
class Product {
static DEFAULT_IMAGE = "default.jpg"
String name
Float basePrice
Category category
String image = DEFAULT_IMAGE
static constraints = {
name(size:3..25, blank:false)
basePrice(scale:2, nullable:false)
category(inList:Category.list(), nullable:false)
image(blank:false)
}
}
productInstance.image = getProductPicturePath() ?: Product.DEFAULT_IMAGE
Even better, make getProductPicturePath()
return the default value.
更好的是,make getProductPicturePath()返回默认值。
#2
If there is no image
key in params
如果params中没有图像键
def productInstance = new Product(params)
will skip this property and leave the default value defined in the domain class. So, assuming that your params doesn't contain an image
key, the simplest way would be to skip the assignment of the image
property at all for those products that have no image:
将跳过此属性并保留域类中定义的默认值。因此,假设您的参数不包含图像密钥,最简单的方法是跳过为没有图像的产品分配图像属性:
def imagePath = getProductPicturePath()
if(imagePath) {
productInstance.image = imagePath
}