Chinaunix首页 | 论坛 | 博客
  • 博客访问: 333117
  • 博文数量: 104
  • 博客积分: 2815
  • 博客等级: 少校
  • 技术积分: 595
  • 用 户 组: 普通用户
  • 注册时间: 2010-05-06 16:32
文章分类

全部博文(104)

文章存档

2013年(1)

2012年(2)

2011年(21)

2010年(80)

我的朋友

分类: 系统运维

2012-01-14 01:19:58

Forwarded form: http://cephas.net/blog/2007/01/04/ognl-getter-and-setter-types-must-match/

Yesterday I ran into a interesting bug with the application I spend my waking hours working on, at least initially I thought it was a WebWork bug. I had a with a getter / setter combination that looked like this:

public void setUser(String username) { ... } public User getUser() { ... }

The thinking here was that if I invoked the action using a request like this:

/myaction.jspa?user=aaron

then the action would look up the user based on the given username and populate a User object, which would then be returned by the getUser() method (populating the user instance using a custom IOC interceptor would have also worked but would have been overkill in this particular case). The problem was that the setUser(String username) method was never getting called. After checking and double checking the method names and the parameter being passed, I googled around and found this email thread which discusses the very issue, saying that the problem was in fact not with WebWork, nor with it’s underlying expression engine , but rather the ‘Java reflection API semantics’. In the thread, Laurie Harper, a Struts committer, says that the:

… restriction (where getter and setter have to be of the same type) comes from the Java relection API semantics, not OGNL. A property can only have one type, so it makes sense that the getter and setter for a JavaBean property must agree on that type.

I didn’t doubt what he said, but I needed to see it with my own eyes, so I dove into the deep sea that is OGNL, reflection and JavaBeans. First I read the , which seems to back up his story: page 55 of the PDF says this:

By default, we use design patterns to locate properties by looking for methods of the form:
public get();
public void set( a);
If we discover a matching pair of â

阅读(1330) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~