Grails域类遇到问题。我重写构造函数从net.sf.json.JSONObject构建域类对象。当我通过控制器检测对象时,这工作正常。然后我尝试通过一个测试用例来实例化它,并且得到一个异常:Grails addTo *未创建
没有签名的方法:profileplugin.Contact.addToEmails()适用于参数类型:(java.lang.String)values:[something @ something.com]
我还应该指出,这似乎适用于某些类,但不适用于其他类。非常令人沮丧 - 我是新来的Grails,所以如果任何人都能指出我正确的方向,我会非常感激。
这是我的域类代码。
package profileplugin
import net.sf.json.JSONObject
class Contact
{
static hasMany =
[
phones: String,
faxes: String,
emails: String,
websites: String,
];
Contact() {}; // standard constructor must be specified, or grails dies
Contact(JSONObject source)
{
source.get('emails').each() { this.addToEmails(it); };
source.get('websites').each() { this.addToWebsites(it); };
source.get('phones').each() { this.addToPhones(it); };
source.get('faxes').each() { this.addToFaxes(it); };
};
}
而且这里有一个例子源JSON字符串...
[
addresses:[],
phones:["(555) 555-7011"],
faxes:[],
emails:["[email protected]"],
websites:["http://www.google.com"]
]
最后,这里是工作的代码的版本(以下获得反馈后):
class Contact
{
def phones = [];
def faxes = [];
def emails = [];
def websites = [];
Contact() {}; // standard constructor must be specified, or grails dies
Contact(JSONObject source)
{
print source;
source.get('phones').each() { this.phones.add(it); };
source.get('emails').each() { this.emails.add(it); };
source.get('websites').each() { this.websites.add(it); };
source.get('faxes').each() { this.faxes.add(it); };
};
}
你能压缩的样本项目,所以我们可以看看? – Mengu 2012-01-13 01:35:44