I have input fields and datatable in the same page, in such a way that when user adds a new data, data should be added to the datatable without refreshing the page.
To achieve this I tried the following code
hospials.xhtml:
<h:form>
<h:outputLabel>Name:</h:outputLabel>
<h:inputText value="#{hospitalsBean.ename}"/>
<h:outputLabel>Address:</h:outputLabel>
<h:inputText value="#{hospitalsBean.hospital.address}"></h:inputText>
<h:commandButton value="A">
<f:ajax event="click" render="table" listener="#{hospitalsBean.command()}"></f:ajax>
</h:commandButton>
</h:form>
<h:dataTable id="table" value="#{hospitalsBean.hospitals}" var="hospital">
<h:column>
<f:facet name="header">Name</f:facet>
#{hospital.name.english}
</h:column>
<h:column>
<f:facet name="header">Address</f:facet>
#{hospital.address}
</h:column>
</h:dataTable>
HospitalsBean.java :
private Hospital hospital = new Hospital();
private ArrayList<Hospital> hospitals = new ArrayList<>();
private Part file; // +getter+setter
public ArrayList<Hospital> getHospitals() {
return MongoCrud.getHospitals();
}
public void setHospitals(ArrayList<Hospital> hospitals) {
this.hospitals = hospitals;
}
public Hospital getHospital() {
return hospital;
}
public void setHospital(Hospital hospital) {
this.hospital = hospital;
}
public void command() throws IOException {
MongoService.insertData("Hospital", Finals.gson.toJson(hospital));
}
but when i insert data to mongodb the hospital variable have no fill name and address