Java 从 JSF 页面调用方法的疑惑

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6594920/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-16 08:04:27  来源:igfitidea点击:

Calling methods from JSF page doubts

javajsfjakarta-eejsf-2el

提问by sfrj

I have a couple of questions about the way I call methods in EL. Maybe someone could explain how it actually works.

我有几个关于我在 EL 中调用方法的方式的问题。也许有人可以解释它实际上是如何工作的。

I did this very simple example:

我做了这个非常简单的例子:

index.xhtml

索引.xhtml

<h:body>
<!-- Using a method -->
#{bba.salute()}
<br/>
<h:outputText value="#{bba.salute()}"/>
<br/>
<!-- Using a method from an injected bean-->
 #{bba.b.doSomething()} 
</h:body>

BackBeanA.java

BackBeanA.java

@Named("bba")
@SessionScoped
public class BackBeanA implements Serializable {

    private static final long serialVersionUID = 5671761649767605303L;
    @Inject
    private BackBeanB b;

    public String salute() {
        return "Hi! I am 'A'";
    }

    public BackBeanB getB() {
        return b;
    }

    public void setB(BackBeanB b) {
        this.b = b;
    }   
}

BackBeanB.java

BackBeanB.java

@Named("bbb")
@SessionScoped
public class BackBeanB implements Serializable {

    private static final long serialVersionUID = -4786092545430477941L;

    public String doSomething() {
        System.out.println("Hello!!!");
        return "I am a SessionScopped Backing Bean, my name is 'B' and i am doing something";
    }
}

This are the questions I have:

这是我的问题:

  1. When I call a method from a backing bean, when do I need to use the brackets (), and when I don't need? Example: If I remove the brackets from #{bba.salute()}, I get an error, that says(Cannot find a property called 'salute')

  2. I also want to learn how to call a method from an injected bean. I injected BackBeanB, inside BackBeanA, but when I say #{bba.salute()}in the page, I don't see the message I from the method in BackBeanB. Why is that? Injected beans don't need to be initialized in @PostConstructright? Are the getters and setters for the injected bean enough?

  3. Note the line where I say <h:outputText value="#{bba.salute()}"/>, it works, but eclipse displays a warning like this:

    enter image description here

    Why is that?

  1. 当我从支持 bean 调用方法时,什么时候需要使用方括号 (),什么时候不需要?示例:如果我从 中删除括号#{bba.salute()},则会收到错误消息,即(无法找到名为“salute”的属性)

  2. 我还想学习如何从注入的 bean 中调用方法。我在 BackBeanA 中注入了 BackBeanB,但是当我#{bba.salute()}在页面中说时,我没有看到来自BackBeanB. 这是为什么?注入的bean不需要在@PostConstruct正确的初始化?注入 bean 的 getter 和 setter 是否足够?

  3. 请注意我说的那一行<h:outputText value="#{bba.salute()}"/>,它可以工作,但是 eclipse 会显示如下警告:

    在此处输入图片说明

    这是为什么?

采纳答案by Romain Linsolas

When you write #{myBean.salute}, JSF is looking for the propertysalute. In Java code, it is "translated" to myBean.getSalute();. In others words, you have to provide the getter for this property (and eventually the setter if this property can be modified by JSF, when it is used in an input field for example).

当您编写时#{myBean.salute},JSF 正在寻找该属性salute。在 Java 代码中,它被“翻译”为myBean.getSalute();. 换句话说,您必须为此属性提供 getter(如果此属性可以由 JSF 修改,则最终是 setter,例如,当它在输入字段中使用时)。

When you write #{myBean.salute()}you are referring to the methodsalute().

当您写作时,#{myBean.salute()}您指的是方法salute()

The rule is quite simple: use a method when you want to do an action (i.e. generally it will be defined inside an actionor actionListenerattribute). In the others cases, use a property. In your example, you want to display some text in your page, so instead calling #{myBean.salute()}, just call #{myBean.salute}.

规则很简单:当你想做一个动作时使用一个方法(即通常它会在一个actionactionListener属性中定义)。在其他情况下,使用属性。在您的示例中,您希望在页面中显示一些文本,因此不要调用#{myBean.salute()},只需调用#{myBean.salute}

For the second point, try to change your code to access the property somethinginstead of the method:

对于第二点,尝试更改代码以访问属性something而不是方法:

<!-- Using a method from an injected bean-->
#{bba.b.something} 

and in BeanBcode:

并在BeanB代码中:

public String getSomething() {
    System.out.println("Hello!!!");
    return "I am a SessionScopped Backing Bean, my name is 'B' and i am doing something";
}

Regarding your last point, I think that your Eclipse simply doesn't handle the EL 2.0 syntax.

关于您的最后一点,我认为您的 Eclipse 根本无法处理 EL 2.0 语法。