gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 03:52:41 24 4
gpt4 key购买 nike

我的代码中发生了神秘的事情。这是 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com