所以我发现了这个问题,这真的很奇怪..
所以我有这个 xhtml 页面:
<h:form>
<rich:dataTable value="#{carsBean.myList}" var="car">
<rich:column>
<f:facet name="header">
<h:outputText value="Name" />
</f:facet>
<h:inputText value="#{car.name}">
<p:ajax event="keyup" listener="#{carsBean.print}" />
</h:inputText>
</rich:column>
<rich:column>
<f:facet name="header">
<h:outputText value="Company" />
</f:facet>
<h:inputText value="#{car.company}">
<p:ajax event="keyup" listener="#{carsBean.print}" />
</h:inputText>
</rich:column>
<rich:column>
<f:facet name="header">
<h:outputText value="Address" />
</f:facet>
<h:inputText value="#{car.address}">
<p:ajax event="keyup" listener="#{carsBean.print}" />
</h:inputText>
</rich:column>
</rich:dataTable>
<h:commandButton value="save-empty">
</h:commandButton>
</h:form>
以下是托管bean:
@ManagedBean(name="carsBean")
@SessionScoped
public class CarsBean implements Serializable {
/**
*
*/
private static final long serialVersionUID = -7818770780064447805L;
private List<Car> myList;
private String test;
public CarsBean(){}
public void print(){
System.out.println("I got hit!");
}
/**
* @return the myList
*/
@PostConstruct
void initialiseSession() {
FacesContext fc = FacesContext.getCurrentInstance();
fc.getExternalContext().getSession(true);
if(myList==null){
myList = new ArrayList<Car>();
};
if(myList.isEmpty()){
for(int a=0;a<10;a++){
myList.add(new Car("test","test","test"));
}
}
}
public List<Car> getMyList() {
return myList;
}
/**
* @param myList the myList to set
*/
public void setMyList(List<Car> myList) {
this.myList = myList;
}
public String getTest() {
return test;
}
public void setTest(String test) {
this.test = test;
}
public static class Car{
private String name;
private String company;
private String address;
public Car(){}
public Car(String name, String company, String address) {
super();
this.name = name;
this.company = company;
this.address = address;
}
/**
* @return the name
*/
public String getName() {
return name;
}
/**
* @param name the name to set
*/
public void setName(String name) {
this.name = name;
}
/**
* @return the company
*/
public String getCompany() {
return company;
}
/**
* @param company the company to set
*/
public void setCompany(String company) {
this.company = company;
}
/**
* @return the address
*/
public String getAddress() {
return address;
}
/**
* @param address the address to set
*/
public void setAddress(String address) {
this.address = address;
}
}
}
现在的目标是将用户输入保存在 sessionscope 中,这样即使用户关闭选项卡或发出新请求,数据也会被保留。
上面的代码有效,但我注意到,当我删除输入框上的 ajax 事件时,它就消失了。
有任何想法吗?
谢谢