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

查看:107
本文介绍了从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()} 时,我在<$中看不到该方法中的消息C $ C> 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正在寻找属性 致敬。在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()} 时,您指的是方法 salute()

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

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

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

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天全站免登陆