I have some JSF page and some manage bean. In manage bean I create some odel file and try to fill it by JSF. But it doesn’t setup values in. But when i use existing model object in works great.
Here is my JSF code:
<h:form>
<c:set var="patient" value="#{manageBean.patient}" />
<p:panel id="panel" header="Patient" style="margin-bottom:10px;">
<h:panelGrid columns="2">
<h:outputLabel value="First name" />
<p:inputText id="firstName" required="true" value="#{patient.firstName}" />
<h:outputLabel value="Family name" />
<p:inputText id="familyName" required="true" value="#{patient.familyName}" />
<h:outputLabel value="Sex"/>
<p:selectOneMenu id="sex" rendered="true" value="#{patient.sex}">
<f:selectItem itemLabel="Male" itemValue="male" />
<f:selectItem itemLabel="Female" itemValue="female" />
</p:selectOneMenu>
<h:outputLabel value="Birthday date" />
<p:calendar value="#{patient.birthdayDate}" mode="inline" id="birthdayDate"/>
<h:outputLabel value="Nationality"/>
<p:selectOneMenu id="nationality" rendered="true" value="#{patient.nationality}">
<f:selectItem itemLabel="Russian" itemValue="russian" />
<f:selectItem itemLabel="Ukranian" itemValue="ukranian" />
</p:selectOneMenu>
<h:outputLabel value="Adress" />
<p:inputText id="adress" required="true" value="#{patient.adress}" />
<h:outputLabel value="Phone number" />
<p:inputMask id="phoneNumber" required="true" value="#{patient.phoneNumber}" mask="(999) 999-9999"/>
</h:panelGrid>
</p:panel>
<p:commandButton value="Save" action="#{manageBean.save}" />
</h:form>
And there is my ManageBean:
@ManagedBean(name = "manageBean")
@SessionScoped
public class ManageBean implements Serializable {
private Patient patient;
private SessionFactory factory;
public ManageBean() {
factory = SessionFactoryWrap.getInstance();
}
public Patient getPatient() {
patient = (Patient) FacesContext.getCurrentInstance().getExternalContext().getSessionMap().get("patient");
if (patient == null) {
//patient = new Patient("", "", Sex.male, new Date(), Nationality.ukranian, "", "");
patient = new Patient();
}
return patient;
}
public String save() {
Session session = factory.openSession();
Transaction tx = null;
try {
tx = session.beginTransaction();
session.saveOrUpdate(patient);
tx.commit();
} catch (HibernateException ex) {
if (tx != null) {
tx.rollback();
}
ex.printStackTrace();
} finally {
session.close();
}
patient=null;
FacesContext.getCurrentInstance().getExternalContext().getSessionMap().put("patient", null);
return "go_home";
}
}
Another way you could solve the problem would be to not using any parameter at all and just binding the
UIInputcomponents to your attribute directly:Also, following JSF best practices, you could redefine your managed bean in two ways (as far as I can see atm):
You don’t need to have the
@SessionScopedannotation in order to handle ajax requests, also it would mean that the constructor (and the@PostConstructmethod) will be called only once per session. The best option for this case would be the@ViewScopedannotation. More info: Managed Bean Scopes.You must not have any business logic in your getter/setter method because it will be executed for every
#{managedBean.property}in your JSF code, more info: Why JSF calls getters multiple times. Knowing this, it would be better to load the session data just once in the bean constructor or in the@PostConstructmethod.With all this, your managed bean should look like: