当属性名称的第二个字符是大写时抛出 javax.el.PropertyNotFoundException

发布于 2024-08-20 08:26:47 字数 614 浏览 10 评论 0原文

我有一个不寻常的场景:

我有一个 RegistrationVO,它的属性和 getter setter 很少。 例如 citybCity 及其 getter 方法 getCity()getBCity()

在 JSP 中我尝试显示使用 scriplet 计算这些属性的值, <%=registrationVO.getCity()%> 和 <%=registrationVO.getBCity()%> ,效果很好。 但我用表达语言替换了相同的内容, ${registrationVO.city}${registrationVO.bCity} 我收到一条错误消息,指出在 RegistrationVO 中未找到属性“bCity”。 ia 再次对 bCity 使用 scriplet,我得到了输出。

我观察到这是因为命名约定。 “如果属性的第二个字符是大写字母,我们就不能使用表达式语言”。我尝试过许多差异命名,这就是我发现的。

请检查一下这个场景,我不知道我的结论是对还是错。

谢谢, DJ

I have this unusual scenario:

I have a registrationVO with few properties and getter setters for that.
For example city or bCity with their getter methods getCity() and getBCity()

In JSP i tried to display the value of these properties using scriplets,
<%=registrationVO.getCity()%> and <%=registrationVO.getBCity()%> , It works fine.
But i replaced the same with expression language,
${registrationVO.city} and ${registrationVO.bCity}
i got an error saying property "bCity" not found in registrationVO.
i a used scriplet again for bCity, i got the output.

I observed that its because of the naming convention. "If the second character of the property is a Capital letter we cant use Expression Language". I have tried with many diff namings, this is what i found out.

Please check this scenario, I don't know wether my conclusion is right or wrong.

Thanks,
DJ

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

如果 getter 方法的属性名称至少以两个大写字符开头,那么您也需要在 EL 属性名称中使用所有这些大写字符。在您的特定情况下,您需要将其替换为 ${registrationVO.BCity}。这是在 Javabeans 规范 的第 8.8 章中指定的。这是本章的摘录(重点是我的):

8.8 推断名称的大写。

当我们使用设计模式来推断属性或事件名称时,我们需要决定什么规则
遵循大写推断的名称。如果我们从正常的中间提取名称
混合大小写样式的 Java 名称,则默认情况下该名称将以大写字母开头。

Java 程序员习惯于使用以小写字母开头的普通标识符。
审稿人的积极意见使我们相信我们应该遵循同样的传统规则
用于属性和事件名称。

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

  • “FooBah”变成“fooBah”
  • “Z”变成“z”
  • “URL”变为“URL”

我们提供了一个方法Introspector.decapitalize来实现此转换规则。

也就是说,我宁愿将它们重命名为更合理的名称。也许是birthCity(如果我猜对的话),这样你就可以很好地使用${registrationVO.birthCity}

If the property name of the getter method starts with at least two uppercase characters, then you need to use all of those uppercase characters in the EL property name as well. In your particular case, you need to replace it by ${registrationVO.BCity}. This is specified in chapter 8.8 of the Javabeans spec. Here's an extract of the chapter (emphasis mine):

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.

That said, I would rather rename them to something more sensible. Maybe birthCity (if I guess it right), so that you can just nicely use ${registrationVO.birthCity}.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文