Hi I am facing an issue in populating data in the edit page when navigated from search page to edit page. I am able to see the employeeDTO
data in employeeBean.edit
method but not available when the edit page rendering completed. Please help me in finding out the issue.
Also please clarify the below.
1) What is the scope of @viewScoped beans when the request life cycle process is executing. does the @viewScoped beans put into request scope?
2) What is the Flash scope in JSF. Do i need to use the flash scope in the above scenario to populate data in the edit page?
Below are the codes.
EmployeeCreate.xhtml(for both create and update operations)
<!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:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:ui="http://java.sun.com/jsf/facelets">
<h:head><title>Employee Registration</title>
<link href="../css/styles.css"
rel="stylesheet" type="text/css"/>
</h:head>
<h:body>
<div align="center">
<h:form>
<h:panelGrid columns="3" styleClass="formTable">
Employee Name:
<h:inputText value="#{employeeBean.employeeDTO.empName}"
required="true"
id="empName">
</h:inputText>
<h:message for="empName" styleClass="error"/>
Employee Location:
<h:inputText value="#{employeeBean.employeeDTO.empLocation}"
required="true"
id="empLocation">
</h:inputText>
<h:message for="empLocation" styleClass="error"/>
</h:panelGrid>
<h:commandButton value="Submit"
action="#{employeeBean.submit}"/>
</h:form>
</div>
</h:body>
</html>
EmployeeSearch.xhtml
<!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:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:ui="http://java.sun.com/jsf/facelets">
<h:head><title>Employee Search Result</title>
<link href="../css/styles.css"
rel="stylesheet" type="text/css"/>
</h:head>
<h:body>
<div align="center">
<h:form>
<h1>Employee List</h1>
<h:dataTable value="#{employeeSearchBean.employeesList}" var="emp">
<h:column>
<!-- column header -->
<f:facet name="header">Employee ID</f:facet>
<!-- row record -->
<h:commandLink value="#{emp.empId}" action="#{employeeBean.edit}">
<f:setPropertyActionListener target="#{employeeBean.employeeDTO}" value="#{emp}" />
</h:commandLink>
</h:column>
<h:column>
<f:facet name="header">Employee Name</f:facet>
#{emp.empName}
</h:column>
<h:column>
<f:facet name="header">Employee Location</f:facet>
#{emp.empLocation}
</h:column>
</h:dataTable>
</h:form>
</div>
</h:body>
</html>
EmployeeBean.java(for Create/Update operations etc)
package com.employee;
import javax.annotation.PostConstruct;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
@ManagedBean
@ViewScoped
public class EmployeeBean {
private EmployeeDTO employeeDTO;
@PostConstruct
public void init(){
System.out.println(">>>>>>> init >>>>");
}
public EmployeeDTO getEmployeeDTO() {
return employeeDTO;
}
public void setEmployeeDTO(EmployeeDTO employeeDTO) {
this.employeeDTO = employeeDTO;
}
public String edit(){
System.out.println(">>> edit employee Id >>> " + employeeDTO.getEmpId() );
return "edit";
}
public String submit(){
// to do
return null;
}
}
EmployeeSearchBean.java (it is the requirement for my project to take separate bean for Search operation)
@ManagedBean
@ViewScoped
public class EmployeeSearchBean {
List<EmployeeDTO> employeesList;
public List<EmployeeDTO> getEmployeesList(){
employeesList = new ArrayList<EmployeeDTO>();
EmployeeDTO emp1 = new EmployeeDTO();
emp1.setEmpId(new Long(1));
emp1.setEmpLocation("Location1");
emp1.setEmpName("Mike");
EmployeeDTO emp2 = new EmployeeDTO();
emp2.setEmpId(new Long(2));
emp2.setEmpLocation("Location2");
emp2.setEmpName("name2");
EmployeeDTO emp3 = new EmployeeDTO();
emp3.setEmpId(new Long(3));
emp3.setEmpLocation("Location3");
emp3.setEmpName("name3");
employeesList.add(emp1);
employeesList.add(emp2);
employeesList.add(emp3);
return employeesList;
}
}
EmployeeDTO.java
public class EmployeeDTO implements Serializable {
private static final long serialVersionUID = 1L;
private Long empId;
private String empName;
private String empLocation;
// setters and getters ........
}
faces-config.xml
<managed-bean>
<managed-bean-name>employeeBean</managed-bean-name>
<managed-bean-class>com.employee.EmployeeBean</managed-bean-class>
<managed-bean-scope>view</managed-bean-scope>
<managed-property>
<property-name>employeeDTO</property-name>
<value>#{employeeDTO}</value>
</managed-property>
</managed-bean>
<managed-bean>
<managed-bean-name>employeeDTO</managed-bean-name>
<managed-bean-class>com.employee.EmployeeDTO</managed-bean-class>
<managed-bean-scope>view</managed-bean-scope>
</managed-bean>
<navigation-rule>
<from-view-id>/jsf/EmployeeSearch.xhtml</from-view-id>
<navigation-case>
<from-outcome>edit</from-outcome>
<to-view-id>/jsf/EmployeeCreate.xhtml</to-view-id>
</navigation-case>
</navigation-rule>
Thanks in advance. Any help appreciated.