gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 23:27:24 25 4
gpt4 key购买 nike

所以我们都知道#{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 willuse 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 anexception, 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/

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