从 JSF 页面调用方法的疑惑 [英] Calling methods from JSF page doubts

查看:16
本文介绍了从 JSF 页面调用方法的疑惑的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

关于我在 EL 中调用方法的方式,我有几个问题.也许有人可以解释它的实际工作原理.

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

这是我的问题:

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

  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')

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

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?

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

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

这是为什么?

推荐答案

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

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

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

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

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

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

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} 

和在 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 语法.

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

这篇关于从 JSF 页面调用方法的疑惑的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆