【发布时间】:2015-02-11 08:11:27
【问题描述】:
我编写了两个 managedbean 类,分别命名为 Message 和 HelloWorld。它们如下:
Message.java:
package com.bean;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.faces.bean.SessionScoped;
@ManagedBean(name = "message", eager = true)
@RequestScoped
@SessionScoped
public class Message {
private String message = "Hello World!";
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
}
HelloWorld.java
package com.bean;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.RequestScoped;
import javax.faces.bean.SessionScoped;
@ManagedBean(name = "helloWorld")
@RequestScoped
@SessionScoped
public class HelloWorld {
@ManagedProperty(value="#{message}")
private Message messageBean;
private String msg;
public HelloWorld() {
System.out.println("HelloWorld started!");
}
public void setMessage(String message) {
this.msg = message;
}
public String getMessage() {
if(messageBean != null){
msg = messageBean.getMessage();
}
return msg;
}
public void setMessageBean(Message message) {
this.messageBean = message;
}
public void showMsg(){
// String str="I am a demo string!";
System.out.println(msg +" I am from showMsg()");
}
}
还有,我的index.xhtml 在这里:
<body>
#{helloWorld.message}
<h:form>
<h:commandButton value="Show Msg" action="#{helloworld.showMsg}"/>
</h:form>
</body>
#{helloWorld.message} 完美打印消息。但是<h:commandButton> does not invoke the methodshowMsg()`。有什么问题?
【问题讨论】:
-
#{helloWorld.showMsg}- 注意大写 W -
您应该使用
RequestScope或SessionScope- 两者一起使用没有任何意义。 -
仅供参考:此外,
eager = true仅适用于应用程序范围的 JSF 托管 bean。就像从@ManagedBean注释中删除一样好。
标签: jsf