4

我有这个不寻常的场景:

我有一个registrationVO,它的属性和getter setter很少。例如citybCity使用他们的 getter 方法getCity()getBCity()

在 JSP 中,我尝试使用脚本显示这些属性的值, <%=registrationVO.getCity()%>并且 < %=registrationVO.getBCity()%>,它工作正常。但是我用表达式语言替换了相同的内容, ${registrationVO.city}并且${registrationVO.bCity} 我收到一个错误,说在registrationVO中找不到属性“bCity”。我再次为 bCity 使用了 scriplet,我得到了输出。

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

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

谢谢,DJ

4

1 回答 1

8

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”</li>
  • “Z” becomes “z”</li>
  • “URL” becomes “URL”</li>

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}.

于 2010-02-05T13:37:36.023 回答