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/56769029/

相关文章:

c# - 以 "Does"开头的方法名称看起来不错吗?

C# 项目文件夹命名约定

java - JCalendar定制

使用 url 编码参数进行 Java URL 编码

java - 在java中使用字符串元素对集合进行排序

java - 覆盖实例化 Java 对象中的方法

java - 为什么有些方法使用 boolean 类而不是原始类型?

java - 使用 BouncyCaSTLe API 时出现 RSA key 生成错误

java - 如何创建一个找到最小数的逻辑方法

css - 为什么 css 对象要以前缀 o- 开头,而组件要以 c- 开头?