4. Getting Started - Simple Form Entry with Manged bean Action in JSF 2.0
Previous
3. Getting Started - Simple Datatable with Managed bean in JSF 2.0
Adding Constructor for New Object Creation
create new empty constructor in Person Class
Person() {
}
Create New Object for Data Model Class
Object Declartion:
Person person;
Object Intiralization with in Conctructor:
person = new Person();
Declare Getter and Setter Method for this Object.
Full Code of Data Model Class
package com.thiyagaraaj.example;
/**
*
* @author thiyagaraaj
* @webiste little drops @ thiyagaraaj.com
*/
public class Person {
String Name;
String No;
String Address;
Person() {
}
public Person(String Name, String No, String Address) {
this.Name = Name;
this.No = No;
this.Address = Address;
}
public String getAddress() {
return Address;
}
public void setAddress(String Address) {
this.Address = Address;
}
public String getName() {
return Name;
}
public void setName(String Name) {
this.Name = Name;
}
public String getNo() {
return No;
}
public void setNo(String No) {
this.No = No;
}
}
Create Action Function in Managed bean Class
Action Method Declation:
public String addPersion() {
personList.add(person);
person = new Person();
return "Success";
}
Full Code of Managed bean Class
package com.thiyagaraaj.example;
import com.thiyagaraaj.example.Person;
import java.util.ArrayList;
import java.util.List;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
/**
*
* @author thiyagaraaj
* @webiste little drops @ thiyagaraaj.com
*/
@ManagedBean
@SessionScoped
public class PersonController {
Person person;
List<Person> personList = new ArrayList<Person>();
/** Creates a new instance of PersonController */
public PersonController() {
person = new Person();
personList.add(new Person("Google", "1", "Search Engine"));
personList.add(new Person("Windows", "2", "Operating System"));
personList.add(new Person("Oracle", "3", "Database"));
personList.add(new Person("Facebook", "4", "Social Networking"));
}
public String addPersion() {
System.out.println("Inside Action");
personList.add(person);
person = new Person();
return "Success";
}
// Getter and Setter Methods
public List<Person> getPersonList() {
return personList;
}
public void setPersonList(List<Person> personList) {
this.personList = personList;
}
public Person getPerson() {
return person;
}
public void setPerson(Person person) {
this.person = person;
}
}
Modify Facelet Page (XHTML) for Backing Bean Variables
<h:outputText value="Name" />
<p:inputText value="#{personController.person.name}"/>
<h:outputText value="No" />
<p:inputText value="#{personController.person.no}"/>
<h:outputText value="Address" />
<p:inputText value="#{personController.person.address}"/>
Call Action From Faclets to Managed Bean
Replace Existing Command Button code to this.
<p:commandButton value="Finish" action="#{personController.addPersion()}" ajax="false" />
Full Code of Facelet View Page
<?xml version='1.0' encoding='UTF-8' ?>
<!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:p="http://primefaces.prime.com.tr/ui"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core">
<h:head>
<title>Facelet Title</title>
</h:head>
<h:body>
<h:form>
<p:panel header="Simple Form in JSF 2.0 - little drops @ thiyagaraaj.com">
<h:panelGrid columns="2">
<h:outputText value="Name" />
<p:inputText value="#{personController.person.name}"/>
<h:outputText value="No" />
<p:inputText value="#{personController.person.no}"/>
<h:outputText value="Address" />
<p:inputText value="#{personController.person.address}"/>
</h:panelGrid>
<f:facet name="footer">
<p:commandButton value="Finish" action="#{personController.addPersion()}" ajax="false" />
</f:facet>
</p:panel>
<p:separator/>
<p:dataTable value="#{personController.personList}" var="person">
<p:column>
<f:facet name="header">Name</f:facet>
<h:outputText value="#{person.name}"/>
</p:column>
<p:column>
<f:facet name="header">Number</f:facet>
<h:outputText value="#{person.no}"/>
</p:column>
<p:column>
<f:facet name="header">Address</f:facet>
<h:outputText value="#{person.address}"/>
</p:column>
</p:dataTable>
</h:form>
</h:body>
</html>
Sample Outputs: