【发布时间】:2011-01-24 13:41:51
【问题描述】:
我创建了一个如下所示的自定义标签:
def textField = { attrs ->
def field = attrs.name.split('\\.')[-1]
log.error("--------- Field is ${field}")
if (attrs.bean && attrs.bean.errors.hasFieldErrors(field)) {
def className = attrs.remove('class')
def classStr = 'errors '
if (className) {
classStr += className
}
attrs.put('class', classStr)
attrs.put('value', attrs.bean[field])
attrs.remove('bean')
}
out << g.textField(attrs)
}
我在 GSP 中这样称呼它:
<my:textField bean="${client}" name="client.firstName"/>
<my:textField bean="${client}" name="client.lastName"/>
...
<my:textField bean="${client}" name="client.workPhone"/>
这是我的域类
class Client {
String email
String address
String city
String state
String zip
String firstName
String lastName
String phone
String workPhone
String mobilePhone
String birthCountry
String citizenshipCountry
String emergencyContactName
String emergencyPhone
String disabilities
String experience
static constraints = {
email(email:true, unique:true, blank:false)
address(blank:false)
city(blank:false)
state(blank:false)
zip(blank:false)
firstName(blank:false)
lastName(blank:false)
phone(blank:false)
emergencyContactName(blank:false)
emergencyPhone(blank:false)
workPhone(blank:true, nullable:true)
mobilePhone(blank:true, nullable:true)
birthCountry(blank:true, nullable:true)
citizenshipCountry(blank:true, nullable:true)
disabilities(blank:true, nullable:true)
experience(blank:true, nullable:true)
}
static mapping = {
disabilities type: 'text'
experience type: 'text'
}
static hasMany = [courses:ClientCourseMap]
}
页面加载正常,除非我实际上有一个“客户端”bean。它一直加载到最后一个标签“client.workPhone”。然后我得到以下异常:
2010-03-06 18:32:35,329 [http-8080-2] ERROR view.GroovyPageView - 错误处理 GroovyPageView:错误执行标签:org.codehaus.groovy.grails.web.taglib.exceptions.GrailsTagException:错误执行标签:groovy.lang.MissingPropertyException:没有这样的属性:类的客户端:com.personal.Client在/Users/dean/Projects/PersonalGrails/grails-app/views/registration/index.gsp :98 在 /Users/dean/Projects/PersonalGrails/grails-app/views/registration/index.gsp:145
问题是在 bean 上调用 hasFieldErrors 时。它在“字段”中传递,应该是“workPhone”。单步调试器显示该字段正是“workPhone”。但是,通过进一步检查 field 变量,它表明 field 的内部值为“client.workPhone”,并且 offset = 7,count = 9,hash = 0。但是,如果您调用 toString(),则会返回“ workPhone”如您所愿。
我想知道 Grails 还是 Spring 没有正确使用这个字符串?看起来它正在尝试使用该字符串的 real 值,而不是关注该字符串的偏移量/计数并取回预期的值。
有人看到我做错了什么吗?或者您知道解决方法吗?我可以提供任何需要的信息,只要问......这让我发疯了!
【问题讨论】: