我编写了两个托管bean类,分别名为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 method
showMsg()`。有什么问题?
您已将action="#{helloworld.showMsg}"
小写字母w
用于世界。EL是区分大小写的。更改为action="#{helloWorld.showMsg}"
。
另外,在注释中告诉您的内容,您不能同时使用@RequestScoped
和@SessionScoped
,只能选择一个。并且,action
属性应解析为String(showMsg()
return void
),用于执行导航。如果您只是想完成某项操作而没有导航,请actionListener
改用。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句