JSF页面调用方法的疑惑
我有几个关于在 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";
}
}
这是我的问题:
当我打电话时来自支持 bean 的方法,什么时候需要使用方括号 (),什么时候不需要?示例:如果我从
#{bba.saute()}
中删除括号,则会收到错误消息(找不到名为“salut”的属性)我还想了解如何从注入的 bean 调用方法。我在 BackBeanA 内部注入了 BackBeanB,但是当我在页面中输入
#{bba.saute()}
时,我没有看到来自BackBeanB
中的方法的消息 I 。这是为什么?注入的 bean 不需要在 @PostConstruct 中初始化,对吗?注入的 bean 的 getter 和 setter 是否足够?请注意我说的行
,它有效,但 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:
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')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 inBackBeanB
. Why is that? Injected beans don't need to be initialized in@PostConstruct
right? Are the getters and setters for the injected bean enough?Note the line where I say
<h:outputText value="#{bba.salute()}"/>
, it works, but eclipse displays a warning like this:Why is that?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
当您编写
#{myBean.salute}
时,JSF 正在寻找属性salute
。在 Java 代码中,它被“翻译”为myBean.getSaute();
。换句话说,您必须提供该属性的 getter(如果该属性可以由 JSF 修改,例如在输入字段中使用该属性,则最终需要提供 setter)。当您编写
#{myBean.salute()}
时,您指的是方法salute()
。规则非常简单:当您想要执行操作时使用方法(即通常它将在
action
或actionListener
属性内定义)。在其他情况下,使用属性。在您的示例中,您希望在页面中显示一些文本,因此只需调用
#{myBean.salute}
即可,而不是调用#{myBean.salute()}
。对于第二点,尝试更改您的代码以访问属性
something
而不是方法:并且在
BeanB
代码中:关于您的最后一点,我认为您的 Eclipse 只是不处理 EL 2.0 语法。
When you write
#{myBean.salute}
, JSF is looking for the propertysalute
. In Java code, it is "translated" tomyBean.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 methodsalute()
.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
oractionListener
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:and in
BeanB
code:Regarding your last point, I think that your Eclipse simply doesn't handle the EL 2.0 syntax.