Am I misunderstanding the JavaBean method naming convention or is this an anomaly?

I have cryptic events in my code. Here's a snippet from a bean:

public List<HelpContentsFrag> getCFrags() { return cFrags; } public void setCFrags(List<HelpContentsFrag> frags) { cFrags = frags; } 

Here is a snippet from my view code (tag file)

 cFrags:[${topic.cFrags}] 

where the theme is an object of type bean.

Here's the error:

 javax.el.PropertyNotFoundException: Property 'cFrags' not found on type com.company.beans.BeanClass 

Another thing to consider. There is a subtle difference in the creator created by the eclipse. Apparently, he did not like the name cFrags. The field name is cFrags, and with each other setter I get a parameter with the same name as the field, and it is set using the agreement this.fieldName = fieldName . You will notice that the eclipse was not associated with this installer.

FYI: all this works fine when I change the getter to getContentsFrag() and refer to it .contentsFrag .

+6
source share
2 answers

I believe you want:

 cFrags:[${topic.CFrags}] 

With capital C. See JavaBeans Spec :

8.8 Capitalization of prospective names.

When we use design patterns to display the name of a property or event, we need to decide which rules to use for the capitalization of the displayed name. If we choose a name from the middle of the usual mixedCase Java style, the name will start with a capital letter by default. Java programmers are used to the fact that regular identifiers begin with lowercase letters. A striking peer reviewer convinced us that we should follow the same usual rule for property names and events.

Thus, when we retrieve a property or event name from the middle of an existing Java name, we usually convert the first character to lowercase. However, to support the random use of all uppercase names, we check to see if the first two characters of the name are uppercase, and if so, leave it alone. For example,

"FooBah" will become "fooBah"
"Z" becomes "z"
"URL" becomes "URL"

We provide an Introspector.decapitalize method that implements this conversion rule.

+12
source

To quote the JavaBeans specification (last updated in 1997):

Thus, when we retrieve a property or event name from the middle of an existing Java name, we usually convert the first character to a lower case. However, to support the random use of all uppercase names, we check to see if the first two characters of the name are uppercase and if so, leave it alone.

This describes how method names are converted to property names. It is not clear that Introspector creates a separate table, which is also used by property search methods>.

You have already discovered one way to avoid the problem. Another is to create a BeanInfo class that contains the correct mappings of property-> methods (the Introspector document describes how to do this).

+8
source

Source: https://habr.com/ru/post/885529/


All Articles