jsp - Java 表达式语言如何解析 boolean 属性? (在 JSF 1.2 中)

标签 jsp jsf boolean el

所以我们都知道#{someBean.value}将尝试获取 someBean 上某些属性的内容调用value .它将寻找 getValue() .但是,如果这个属性是 boolean 怎么办? ?它将寻找 isValue() .它不会寻找的是hasValue() .

这让我思考,它到底是做什么的?

Java EE 5 tutorial chapter - Unified Expression LanguagePageContext.FindAttribute() . PageContext将您发送至 JSPContext .他们都没有真正解释他们遵循的规则来确定他们正在寻找的方法的名称。

也很容易找到说明方法名称必须以 get 开头的文档。但是,我知道 isValue()作品。

任何人都可以指出我写下来的文档。我不是在寻找我正在寻找引用的教程或示例。

最佳答案

它在 JavaBeans Spec 中都有权威记录。和 EL Specification.
boolean以属性为例,它在 JavaBeans 规范的第 8.3.2 章中有描述:

8.3.2 Boolean properties

In addition, for boolean properties, we allow a getter method to match the pattern:

public boolean is<PropertyName>();

This “is<PropertyName>” method may be provided instead of a “get<PropertyName>” method, or it may be provided in addition to a “get<PropertyName>” method.

In either case, if the “is<PropertyName>” method is present for a boolean property then we will use the “is<PropertyName>” method to read the property value.

An example boolean property might be:

    public boolean isMarsupial();
    public void setMarsupial(boolean m);

所以,#{bean.marsupial}完全期望上面的 getter/setter 对。
在 EL 规范的第 1.23.5 章中:

1.23.5 Coerce A to Boolean or boolean

  • If A is null and the target type is not the primitive type boolean, return null
  • If A is null or "", return false
  • Otherwise, if A is a Boolean, return A
  • Otherwise, if A is a String, and Boolean.valueOf(A) does not throw an exception, return it
  • Otherwise, error

也可以看看:
  • javax.el.PropertyNotFoundException: Property 'foo' not readable on type java.lang.Boolean
  • Java: how to name boolean properties
  • 关于jsp - Java 表达式语言如何解析 boolean 属性? (在 JSF 1.2 中),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3236929/

    相关文章:

    java - 使用 import 后出现 JSP Class Not Found 异常

    java - 如何将 JSF 组件绑定(bind)到支持 bean 属性

    java - 如何在jsf中获取当前 View id和上一个 View id

    java - 将默认值设置为丰富的:select component which has enableManualInput set to true?

    Java:使用字符串作为源代码的一部分

    mysql - Rails boolean 值 - True 和 False 与 1 和 0

    java - 使用 "JSF"或 JSTL 标签迭代 java.util.Map 时出现问题

    java - 将 HTML 下拉值传递给 JSTL sql :query

    java - 使用从 Servlet 到 JSP 的数据填充选择菜单

    javascript - & 1 JavaScript。它是如何工作的?聪明还是善良?