0

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.

RaoPotla
  • 107
  • 2
  • 13

1 Answers1

2

What is the scope of @viewScoped beans when the request life cycle process is executing. does the @viewScoped beans put into request scope?

Nope. They're put into the view scope. Only @RequestScoped beans are put into the request scope. The view scope has the same lifespan as a JSF view, not a HTTP request. See also How to choose the right bean scope?


What is the Flash scope in JSF.

The flash scope lives as long as a single redirect. So, if you send a redirect from page A to page B and put something in flash scope in page A before redirect, then it's only and only available in the redirected request to page B and not in any other request. The flash scope is however not a JSF managed bean scope. The flash scope is represented by the map as available by ExternalContext#getFlash() in backing bean and #{flash} in EL.


Do i need to use the flash scope in the above scenario to populate data in the edit page?

That would be one way, yes. You're however not sending a redirect. You're just returning a different view within the same request (and thus implicitly creating a different view scope). You'd better pass the data via the request scope. Either via ExternalContext#getRequestMap(), or much better via a plain vanilla GET link instead of a POST form. See also the master-detail example here: Creating master-detail pages for entities, how to link them and which bean scope to choose.

Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • could you explain from where the data of employeeBean.employeeDTO retrieved to display in the edit page while Render Response phase is executing. In my case, as u explained above, as i am navigating from search page to edit page, the employeeBean available in search page view will not be available in edit page view(the reason is for the new edit page view, new view map is created), is it correct? – RaoPotla Jan 22 '14 at 06:24
  • Click the link in bottom of the answer and substitute "Product" with "Employee". – BalusC Jan 22 '14 at 06:24
  • thanks @BalusC, by using plain GET request with and f:viewParam, i am able to populate data in the edit page. But i would like to know the how & where these things are implemented in Render Response phase with JSF Mojarra 2.1 implementation. could u please suggest any documents/codes. – RaoPotla Jan 22 '14 at 07:25
  • I have no idea what you're concretely asking. – BalusC Jan 22 '14 at 07:55
  • Hi, @BalusC, in the EmployeeCreate.xhtml(for both create and update operations) i have mentioned the to set the request parameter values to employeeBean.employeeDTO properties. When these request parameter values are set to employeeBean.employeeDTO properties, will the employeeBean be referenced from viewMap created for the new view(i.e EmployeeCreate.xhtml)? Also please give your suggestions on how we can implement above scenario using Flash as you mentioned in the answer. – RaoPotla Jan 22 '14 at 09:32
  • Hi @BalusC, As per the requirement with my project, i am not supposed to work with GET requests by passing the data in the URL for security concerns. could you please provide the alternative approach for the above scenario using Flash as per your replay saying that **That would be one way**. – RaoPotla Jan 22 '14 at 15:08
  • I'd rather use the request map approach. Just put data in there in action method of source bean and extract data from there in `@PostConstruct` of the target bean. – BalusC Jan 22 '14 at 17:41
  • Thanks BalusC. i have tired by putting the the employeeBean in requestMap instead of Flash, in the edit method of employeeBean, and again in @postConstruct init() method i retrieved from the requestMap and copied the properties to _this_ variable. it is working fine. please clarifiy why should we prefer requestMap rather Falsh, are there any disadvantages with Flash if i use in the above scenario. – RaoPotla Jan 23 '14 at 06:12
  • @Balusc : Can you please help me regarding this issue http://stackoverflow.com/questions/20089815/login-logout-issue-with-play-framework-java – Venkaiah Yepuri Jan 23 '14 at 06:21
  • Flash is just the wrong tool for the job. You namely don't need to send a redirect here. If you **need** to send a redirect, then Flash would be the right tool. Try to keep things as simple as possible. That you can use a hammer to fasten a screw doesn't necessarily mean that it should be preferred over a screwdriver. – BalusC Jan 23 '14 at 06:23
  • Got it. Thanks BalusC for the long support. – RaoPotla Jan 23 '14 at 07:18