简体   繁体   English

Java Beans - 如何在JSF页面忽略boolean isXXXXMethod而支持BOOLEAN getXXXXMethod?

[英]Java Beans - how to ignore boolean isXXXXMethod in favor of BOOLEAN getXXXXMethod in JSF page?

I have this MyHumbleTO which attribute is read on a JSF page.我有这个 MyHumbleTO,它的属性在 JSF 页面上读取。 So I need to add isTheAwesomeBoolean Method to use elsewhere.所以我需要添加isTheAwesomeBoolean方法以在其他地方使用。 But now the page is reading that new method instead the get method, which is inappropriate.但是现在页面正在读取那个新方法而不是 get 方法,这是不合适的。 Is there a way to ignore that in page?有没有办法在页面中忽略它?

<h:outputText value="#{myAwesomeBean.myHumbleTO.theAwesomeBoolean ne null ? 
                    (myAwesomeBean.myHumbleTO.theAwesomeBoolean ? 'Yes ' : 'No') : ''}" />


public class MyHumbleTO implements Serializable{

    private Boolean theAwesomeBoolean;

    public boolean isTheAwesomeBoolean() {
        return Boolean.TRUE.equals(theAwesomeBoolean);
    }

    public Boolean getTheAwesomeBoolean() {
        return theAwesomeBoolean;
    }
}

First of all, If you'd try to debug this, you will find out, that this is all about EL implementation, not JSF.首先,如果你尝试调试它,你会发现,这都是关于 EL 实现的,而不是 JSF。

Since there is a right answer from DaniEll for exactly what you pasted in XHTML, I'll answer differently.由于DaniEll对您在 XHTML 中粘贴的内容有一个正确的答案,因此我会做出不同的回答。

No, there is no way to favor one or another.不,没有办法偏袒一个或另一个。 Especially it will never work if you would like to use this in an input.特别是如果你想在输入中使用它,它永远不会工作。 You cannot bind value property like that.您不能那样绑定value属性。 And I'd avoid mixing simple types with class types in such cases (boolean/Boolean, int/Integer etc.).在这种情况下,我会避免将简单类型与 class 类型混合使用(boolean/Boolean、int/Integer 等)。

With recent EL (Version >= 2.2) just use getTheAwesomeBoolean() (see this answer for details).对于最近的 EL(版本 >= 2.2),只需使用getTheAwesomeBoolean() (有关详细信息,请参阅此答案)。

In the given example:在给定的示例中:

<h:outputText value="#{myAwesomeBean.myHumbleTO.getTheAwesomeBoolean() ne null ? 
        (myAwesomeBean.myHumbleTO.theAwesomeBoolean ? 'Yes ' : 'No') : ''}" />

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM