我有一个具有整数属性的实体,在原型代码中看起来像这样:
class MyEntity:
String name
@Choices({1, "BSD", 2, "Apache", 3, "GPL"}
Integer frequency
@ChoicesSegment({1000, 2000, 3000}, {"BSD", "Apache", "GPL"})
Integer type
String getFrequency()
return getChoice("frequency", frequency)
String getType()
return getChoice("type", type)
也许这个解决方案更可行:
class MyEntity:
String name
final static private Something? frequencyChoices = {1000, 2000, 3000}, {"BSD", "Apache", "GPL"}
Integer frequency
final static private String[] typeChoices = new String[] {"BSD", "Apache", "GPL"}
Integer type
@Choices(MyEntity.frequencyChoices)
String getFrequency()
return frequency)
@IntervalChoices(MyEntity.typeChoices)
String getType()
return type
*get** 访问器根据此表返回字符串。
value(type) HumanReadableString(type)
1 BSD
2 Apache
3 GPL
min frequency max frequency HumanReadableString(frequency)
0 1000 rare
1000 2000 frequent
2001 3000 sexy
应该可以获得属性可以采用的所有可能值,例如:
getChoices(MyEntity, "type") returns ("rare", "frequent", "sexy")
应该可以从字符串中获取绑定值:
getValue(MyEntity, "frequency", "sexy") returns (2000,3000)
编辑:所有这些的目的这些方法应该简化表单和请求的生成(当然这不应该是视图实现绑定)
编辑:添加了我想如何告诉 Java 某些属性是特殊的,以便它可以相应地生成 get* 访问器。
编辑:添加了如何在代码中提交选项
编辑:我存储在数据库中的唯一东西是整数,当我想打印它们时,它们应该以某种方式转换为人类可读的字符串。