Grails 如何将字符串转换为枚举?
我的枚举中有一个自定义 toString
方法:
enum TaxRate implements Serializable {
RATE23(23.0),
...
private String s
private BigDecimal rate
private TaxRate(BigDecimal s) {
this.s = s + "%"
this.rate = s * 0.01
}
public String toString() {
return s
}
现在,当我在 HTML 中显示费率时,我会得到很好的输出,如 TAX: 23.0%。
但是,当用户从
我应该重写什么来支持此自定义映射?尝试覆盖 valueOf(String)
以错误结束。
I have a custom toString
method in my enum:
enum TaxRate implements Serializable {
RATE23(23.0),
...
private String s
private BigDecimal rate
private TaxRate(BigDecimal s) {
this.s = s + "%"
this.rate = s * 0.01
}
public String toString() {
return s
}
Now when I display the rates in HTML I get nice output like TAX: 23.0%.
But what happens when a user selects the tax from a <select>
and the sent value is i.e. 23.0%
is that Grails can't create/get the TaxRate
instance...
What should I override to support this custom mapping? Trying to override valueOf(String)
ended with a error..
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您是否看过本页底部的条目?
Have you seen the entry at the bottom of this page?