如何从 Grails 中的控制器访问域属性?

发布于 2024-07-23 07:56:10 字数 784 浏览 5 评论 0原文

我有以下 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 返回一个图像路径,但如果没有提交图像,控制器应将 null 值替换为默认值。 虽然我当然可以写这样的东西:

productInstance.image = getProductPicturePath() ?: "default.jpg"

它肯定不是很干燥,而且我更愿意将该默认值保留在一处。 我怎样才能实现这个目标?

I have the following Grails domain class:

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:

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:

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

林空鹿饮溪 2024-07-30 07:56:11

显而易见的解决方案是将其声明为常量,然后引用它。

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

更好的是,让 getProductPicturePath() 返回默认值。

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.

驱逐舰岛风号 2024-07-30 07:56:11

如果 params 中没有 image 键,

def productInstance = new Product(params)

将跳过此属性并保留域类中定义的默认值。 因此,假设您的参数不包含 image 键,最简单的方法是对于那些没有图像的产品完全跳过 image 属性的分配:

def imagePath = getProductPicturePath()
if(imagePath) {
    productInstance.image = imagePath
}

If there is no image key in 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
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文