如何在Grails中保存对象 [英] How to save object in grails

查看:131
本文介绍了如何在Grails中保存对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有Grails 2.0.4应用程序,我有以下新的领域类,其中包含大约50个属性

I have grails 2.0.4 app, I have new domain class as below, which contains around 50 properties

class Test{
  int testField1
  int testField2
  int testField2
   .
   .
  int testFieldN
}

我想这样做, p>

And I want to do as follows,

 Display Value         Value to Save in DB

'Excellent'             10
'Good'                  8
'Average'               6
'Poor'                  4
'Pathetic'              2

我有一个包含所有这些属性的html表单。

I have a html form which contains all these properties.

如果testField1的值是Display Value的任何值,那么要保存的值将是在保存在数据库中的值中列出的相应值

If testField1 value is any of the value of "Display Value" then the value to be saved will be the corresponding value listed in "Value to Save in DB"

例如如果testField1的值为优秀,则要保存的值为10

这个特殊映射适用于域类中的大约30个属性。

This particular mapping applies to around 30 properties in the domain class.

像这样,我有不同的映射不同的属性。

Like this I have different mappings for different properties.

如何在grails中实现这一点。

How to achieve this in grails.

推荐答案

使用枚举。

I suggest to use enums.

class Test{
  enum Scales{ 
    Excellent(10), Good(8), Average(6), Poor(4), Pathetic(2)
    private final int value
    Scales(int v){ this.value = v}
    int getValue(){ this.value}
  }

  int testField1
  int testField2
  int testField2
   .
   .
  int testFieldN
} 

GSP p>

GSP

<g:select name='testField1' from="${Test.Scales}" optionKey="value"/>

但更好地使用枚举作为一种属性

But better use enum as a type of property

class Test{
  enum Scales{ 
    Excellent(10), Good(8), Average(6), Poor(4), Pathetic(2)
    private final int value
    Scales(int v){ this.value = v}
    int getValue(){ this.value}
  }

  Scales testField1 
  ....
}

然后
GSP

<g:select name='testField1' from="${Test.Scales}"/>

这篇关于如何在Grails中保存对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆