java - 我是不是误解了 Java Bean 方法命名约定,或者这是一种异常情况?

标签 java methods naming-conventions javabeans

我的代码中发生了神秘事件。这是 bean 的片段:

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

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

这是我的 View 代码(标记文件)中的片段

cFrags:[${topic.cFrags}]

其中 topic 是 bean 类型的对象。

这是错误:

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

还有一件事需要考虑。 eclipse 生成的 setter 存在细微差别。显然,它也不喜欢 cFrags 这个名字。字段名称是 cFrags,对于其他所有 setter ,我都获得与字段同名的参数,并且使用约定 this.fieldName = fieldName 进行设置。您会注意到 eclipse 并没有在这个 setter 上坚持这一点。

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

最佳答案

我相信你想要:

cFrags:[${topic.CFrags}]

大写字母 C。参见 JavaBeans Spec :

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.

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” becomes “fooBah”
“Z” becomes “z”
“URL” becomes “URL”

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

关于java - 我是不是误解了 Java Bean 方法命名约定,或者这是一种异常情况?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5599369/

相关文章:

java - 递归isSubtring方法java

c# - 在 C# 属性 setter 中为 ArgumentException 分配给 'paramName' 参数的值?

java - 如何避免警报管理器以相同的时间间隔动态使用不同的消息覆盖?

java - Android中如何将popupMenu添加到listView的项目中?

java - 使用java创建凯撒密码方法

coding-style - 如何命名类似工厂的方法?

javascript - Facebook 样式命名约定

java - 计算大根 : bigdecimal/java

java - jdbc ODBC 连接

javascript - 编写 Array.every() 函数