在JBoss AS 7.1.0.Final上部署.
我有一个非常简单的测试应用程序.它正在按预期工作直到前一天(着名的最后一句话)并且不再做最基本的事情,即设置输入组件的值并在动作组件中使用它.我把这件事情剥夺了基础,无法弄清楚发生了什么.
index.xhtml在这里
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:h="http://java.sun.com/jsf/html">
<h:head>
<title>contacts</title>
</h:head>
<h:form>
<h:outputLabel value="Message:" />
<h:inputText value="#{contactView.siteCode}" />
<h:commandButton action="#{contactView.save}" value="Save" />
</h:form>
</html>
Run Code Online (Sandbox Code Playgroud)
ViewScoped bean就在这里
@Named
@ViewScoped
public class ContactView implements Serializable {
public ContactView() {
}
private String siteCode;
public String getSiteCode() {
System.out.println("getSiteCode: "+ siteCode);
return siteCode;
}
public void setSiteCode(String siteCode) {
System.out.println("setSiteCode: "+ siteCode);
this.siteCode = siteCode;
}
public String save(){
System.out.println("Saving sitecode: " + siteCode);
return "index.jsf";
}
}
Run Code Online (Sandbox Code Playgroud)
我究竟做错了什么?当我点击保存按钮时,我在输出中得到了这个
10:50:37,663 INFO [stdout] (http--0.0.0.0-8080-2) setSiteCode: 22
10:50:37,663 INFO [stdout] (http--0.0.0.0-8080-2) Saving sitecode: null
10:50:37,663 INFO [stdout] (http--0.0.0.0-8080-2) getSiteCode: null
Run Code Online (Sandbox Code Playgroud)
那是因为bean是由CDI管理的@Named,而不是由JSF管理的@ManagedBean.包的JSF范围注释javax.faces.bean仅适用于由JSF管理的bean.在CDI托管bean上,您需要使用CDI注释javax.enterprise.context.但是,CDI没有视图范围的概念.最近的是@ConversationScoped,但管理起来更复杂.如果未在CDI托管bean上指定范围,则默认为请求范围.
确保您的bean在您想要使用时由JSF管理@ViewScoped.
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
@ManagedBean
@ViewScoped
public class ContactView implements Serializable {
// ...
}
Run Code Online (Sandbox Code Playgroud)
此外,还需要确保您的操作方法返回null或void每当您想要保留视图范围时.
| 归档时间: |
|
| 查看次数: |
4504 次 |
| 最近记录: |