JSF页面调用方法的疑惑

发布于 2024-11-18 19:54:04 字数 1787 浏览 4 评论 0原文

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

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

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

@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

@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";
    }
}

这是我的问题:

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

  2. 我还想了解如何从注入的 bean 调用方法。我在 BackBeanA 内部注入了 BackBeanB,但是当我在页面中输入 #{bba.saute()} 时,我没有看到来自 BackBeanB 中的方法的消息 I 。这是为什么?注入的 bean 不需要在 @PostConstruct 中初始化,对吗?注入的 bean 的 getter 和 setter 是否足够?

  3. 请注意我说的行 ,它有效,但 Eclipse 显示如下警告:

    在此处输入图像描述

    为什么会这样?

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

I did this very simple example:

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

@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

@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 @PostConstruct right? 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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

落花随流水 2024-11-25 19:54:04

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

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

规则非常简单:当您想要执行操作时使用方法(即通常它将在 actionactionListener 属性内定义)。在其他情况下,使用属性。
在您的示例中,您希望在页面中显示一些文本,因此只需调用 #{myBean.salute} 即可,而不是调用 #{myBean.salute()}

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

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

并且在 BeanB 代码中:

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

关于您的最后一点,我认为您的 Eclipse 只是不处理 EL 2.0 语法。

When you write #{myBean.salute}, JSF is looking for the property salute. 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).

When you write #{myBean.salute()} you are referring to the method 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 action or actionListener attribute). 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}.

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

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

and in BeanB code:

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文