我有一个简单的添加页面,可以在数据库中添加新记录。页面代码如下,
<h:form id="add_form">
<h:inputText id="patientId" value="#{patientBean.patientId}" label="ID" required="true"/>
<h:inputText id="patientName" value="#{patientBean.patientName}" label="Name" required="true"/>
<h:inputText id="patientDesease" value="#{patientBean.patientDesease}" label="Desease" required="true"/>
<h:commandButton value="Submit" action="#{patientBean.addPatient()}" />
</h:form>
faces-config.xml中
<managed-bean>
<managed-bean-name>patientBean</managed-bean-name>
<managed-bean-class>com.sunnus.pims.managedbean.PatientBean</managed-bean-class>
<managed-bean-scope>session</managed-bean-scope>
<managed-property>
<property-name>patientsBo</property-name>
<value>#{patientsBo}</value>
</managed-property>
</managed-bean>
托管bean类
public class PatientBean implements Serializable{
/**
*
*/
PatientsBo patientsBo;
List<Patient> patients;
List<Patient> filteredPatients;
public List<Patient> getFilteredPatients() {
return filteredPatients;
}
public void setFilteredPatients(List<Patient> filteredPatients) {
this.filteredPatients = filteredPatients;
}
private String patientId;
private String patientName;
private String patientDesease;
public String addPatient() {
Patient patient = new Patient();
patient.setPatientName(getPatientName());
patient.setPatientId(getPatientId());
patient.setPatientDesease(getPatientDesease());
patientsBo.addPatient(patient);
clearPatientBean();
return "";
}
private void clearPatientBean() {
setPatientDesease("");
setPatientId("");
setPatientName("");
}
public PatientBean() {
}
public void setPatientsBo(PatientsBo patientsBo) {
this.patientsBo = patientsBo;
patients = patientsBo.findAllPatient();
}
public String getPatientId() {
return patientId;
}
public void setPatientId(String patientId) {
this.patientId = patientId;
}
public String getPatientName() {
return patientName;
}
public void setPatientName(String patientName) {
this.patientName = patientName;
}
public String getPatientDesease() {
return patientDesease;
}
public void setPatientDesease(String patientDesease) {
this.patientDesease = patientDesease;
}
public List<Patient> getAllPatientList() {
return patientsBo.findAllPatient();
}
}
三个输入框和一个命令按钮。问题是当我第一次输入值并单击“提交”按钮时,没有任何反应。但是当我填充值并在第二次单击按钮时它会成功。