This week's book giveaway is in the OCPJP forum.
We're giving away four copies of OCA/OCP Java SE 7 Programmer I & II Study Guide and have Kathy Sierra & Bert Bates on-line!
See this thread for details.
The moose likes JSF and the fly likes JSF uses reflection for backing bean members? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Java » JSF
Bookmark "JSF uses reflection for backing bean members?" Watch "JSF uses reflection for backing bean members?" New topic
Author

JSF uses reflection for backing bean members?

Paul Smiley
Ranch Hand

Joined: Jun 02, 2000
Posts: 244
Weird problem. In my backing bean, I have a private String called mName and getters and setters called setMName() and getMName(). JSF can't resolve this in a page at runtime - I try to access

<h utputText value="#{bean.mName}" />

and it blows up. Now, if I call

<h utputText value="#{bean.MName}" />

it works just fine. Is this a bug or is my understanding buggy? I thought JSF uses reflection on data members then looks for the proper accessors, but it must not somehow. Anyone see this before?
[ September 07, 2005: Message edited by: Paul Smiley ]
Gregg Bolinger
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15299
    
    6

I really don't know the reason behind this but the correct access method syntax for mName is:

getmName() and setmName(String mName)

If you code it this way your backing bean, it will work the way you expect with value="#{bean.mName}".

I might also point out that prefixing variables as such is a rather old convention that I rarely see followed outside of the C world. Not that there is anything wrong with it, but as you can see, it makes things like this more difficult. I'd just lose them if you can.


GenRocket - Experts at Building Test Data
Paul Smiley
Ranch Hand

Joined: Jun 02, 2000
Posts: 244
The Hibernate/EJB team here has control over the pojo, so the only thing I can do is use the "bean.MName" for now and recommend the change to getmName() syntax.

I hate the naming also, but our hands are tied on that too.

Thanks!
Gregg Bolinger
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15299
    
    6

You could get the source and do a quick refactor if you have a good IDE. Maybe they would never notice.
Paul Smiley
Ranch Hand

Joined: Jun 02, 2000
Posts: 244
When I get a shot, I'll do that. Looks like the culprit is good old Eclipse when you generate getters and setters it does it improperly.

GoodIDE=false;

Paul Smiley
Ranch Hand

Joined: Jun 02, 2000
Posts: 244
Originally posted by Gregg Bolinger:
I really don't know the reason behind this but the correct access method syntax for mName is:

getmName() and setmName(String mName)



Hey Greg, BTW, do you know where I can find this in the spec in writing? It would be good ammo to have
Gregg Bolinger
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15299
    
    6

Originally posted by Paul Smiley:


Hey Greg, BTW, do you know where I can find this in the spec in writing? It would be good ammo to have


I'll see what I can find...
Gregg Bolinger
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15299
    
    6

Originally posted by Paul Smiley:
When I get a shot, I'll do that. Looks like the culprit is good old Eclipse when you generate getters and setters it does it improperly.

GoodIDE=false;



I figured it out because Intellij created them the correct way. You might post something in the IDE forum about eclipse doing it wrong and see what you get back.
Paul Smiley
Ranch Hand

Joined: Jun 02, 2000
Posts: 244
Originally posted by Gregg Bolinger:


I figured it out because Intellij created them the correct way. You might post something in the IDE forum about eclipse doing it wrong and see what you get back.


Good idea.
Paul Smiley
Ranch Hand

Joined: Jun 02, 2000
Posts: 244
I still don't know if IntelliJ and JSF are wrong, or Eclipse is. Looking through the code, it appears as if the former are...
Eric Coelho
Greenhorn

Joined: Feb 07, 2013
Posts: 1
From the book "Core JavaServer Faces":

Note that the name of the property is the “decapitalized” form of the part of the method name that follows the get or set prefix. For example, getFoo gives rise to a property named foo, with the first letter turned into lowercase. However, if the first two letters after the prefix are uppercase, then the first letter stays unchanged. For example, the method name getURL defines a property URL and not uRL.

So in your case, JSF searches for a property called MName.
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 16158
    
  21

Welcome to the JavaRanch, Eric!

What you have done is something we call "Waking a Zombie". Usually, after a thread has gone dormant for a long time, it's considered "dead". We don't actually de-activate it, but we discourage adding to it, since the people involved in the thread have probably lost interest. We'd rather have a new thread that resurrect an old one.

This particular thread has been dead so long that probably not only has everyone moved on to new projects and new jobs, but it's even possible one or more participants could have retired and lost interest in the subject entirely.


Customer surveys are for companies who didn't pay proper attention to begin with.
 
 
subject: JSF uses reflection for backing bean members?