我是不是误解了JavaBean方法的命名约定,还是很奇怪? [英] Am I misunderstanding the JavaBean method naming convention or is this an anomoly?

查看:91
本文介绍了我是不是误解了JavaBean方法的命名约定,还是很奇怪?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的代码中发生了一些不可思议的事情.这是Bean的代码段:

I have mysterious happenings in my code. Here's the snippet from the bean:

public List<HelpContentsFrag> getCFrags()
{
    return cFrags;
}

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

这是我的查看代码(标记文件)中的代码段

Here's the snippet from my view code (tag file)

cFrags:[${topic.cFrags}]

其中topic是Bean类型的对象.

where topic is an object of the bean type.

这是错误:

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

还要考虑的另一件事.蚀产生的二传手之间有细微的差别.显然,它也不喜欢cFrags这个名字.字段名称为cFrags,每隔一个setter我都会得到一个与字段名称相同的参数,并使用约定this.fieldName = fieldName进行设置.您会注意到,月食不符合此设置器上的要求.

One additional thing to consider. There is a subtle difference in the eclipse-generated setter. Apparently, it didn't like the name cFrags either. The field name is cFrags and with every other setter I get parameter with the same name as the field and it is set using the convention this.fieldName = fieldName. You'll notice that eclipse did not stick with that on this setter.

仅供参考:当我将吸气剂更改为getContentsFrag()并引用它为.contentsFrag时,这一切都很好.

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

推荐答案

我相信你想要

cFrags:[${topic.CFrags}]

带有大写字母C.请参见 JavaBeans规范 :

With a capital C. See JavaBeans Spec:

8.8推断名称的大写.

当我们使用设计模式来推断属性或事件名称时,我们需要确定遵循大写推断名称的规则.如果我们从普通的MixedCase样式Java名称的中间提取名称,则默认情况下,该名称将以大写字母开头. Java程序员习惯于使普通标识符以小写字母开头.审阅者的大力投入使我们相信,对于属性和事件名称,我们应该遵循相同的常规规则.

8.8 Capitalization of inferred names.

When we use design patterns to infer a property or event name, we need to decide what rules to follow for capitalizing the inferred name. If we extract the name from the middle of a normal mixedCase style Java name then the name will, by default, begin with a capital letter. Java programmers are accustomed to having normal identifiers start with lower case letters. Vigorous reviewer input has convinced us that we should follow this same conventional rule for property and event names.

因此,当我们从现有Java名称的中间提取属性或事件名称时,通常会将第一个字符转换为小写.但是,为了支持偶尔使用所有大写名称,我们会检查名称的前两个字符是否均为大写,是否保留为大写.例如,

Thus when we extract a property or event name from the middle of an existing Java name, we normally convert the first character to lower case. However to support the occasional use of all upper-case names, we check if the first two characters of the name are both upper case and if so leave it alone. So for example,

"FooBah"成为"fooBah"
"Z"变成"z"
"URL"变成"URL"

"FooBah" becomes "fooBah"
"Z" becomes "z"
"URL" becomes "URL"

我们提供了Introspector.decapitalize方法,该方法实现了此转换规则.

We provide a method Introspector.decapitalize which implements this conversion rule.

这篇关于我是不是误解了JavaBean方法的命名约定,还是很奇怪?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆