0

officeProductionBean.officeName loads only after I manually refresh the page. I am using @Managedbean and @SessionScoped annotations with Primefaces 5.3.

xhtml page Value passed from another page

  <f:metadata>
        <f:viewParam name="id" value="#{officeProductionBean.officeCode}" />
    </f:metadata>

<h:form>
        <p:dataTable value="#{officeProductionBean.officeProductions}" var="officeProductions">

            <f:facet name="header">
                <h:outputText value="Office Production for: #{officeProductionBean.officeName}"/>

            </f:facet>
            <p:column headerText="Total" >
                <h:outputText value="#{officeProductions.total}" />
            </p:column>

            <p:column headerText="Volume" >
                <h:outputText value="#{officeProductions.volume}">
                    <f:convertNumber type="currency" currencySymbol="$" />
                </h:outputText>
            </p:column>

            <p:column headerText="Closing Date" >
                <h:outputText value="#{officeProductions.closingDate}" />
            </p:column>

        </p:dataTable>
    </h:form>

Bean

     @ManagedBean
     @SessionScoped
     public class OfficeProductionBean implements Serializable {

     private static final long serialVersionUID = 1L;

             [clip]

    public List<OfficeProduction> getOfficeProductions() throws ClassNotFoundException, SQLWarning, SQLException {
    Connection connect = null;

    try {
        Class.forName("com.mysql.jdbc.Driver");
        connect = DriverManager.getConnection(URL, USERNAME, PASSWORD);

    } catch (SQLException ex) {
        System.out.println("in exec");
        System.out.println(ex.getMessage());
    }

    officeProductions = new ArrayList<>();
    PreparedStatement ps = connect.prepareStatement("CALL OfficeMonthlyProduction(?);");
    ps.setString(1, officeCode);


    ResultSet rs = ps.executeQuery();

    barModel = new BarChartModel();
    ChartSeries office = new ChartSeries();

    while (rs.next()) {
        total = rs.getString("Total");
        volume = rs.getString("Volume");
        officeName = rs.getString("officename");
        closingDate = rs.getString("Closing Date");

        OfficeProduction officeProduction = new OfficeProduction(total, volume, closingDate, officeName);
        officeProductions.add(officeProduction);

        // Set data points for chart
        office.set(closingDate, Integer.parseInt(total));

    }

    office.setLabel(officeName + " Production");
    barModel.addSeries(office);

    barModel.setTitle("Monthly Production");
    barModel.setLegendPosition("ne");

    Axis xAxis = barModel.getAxis(AxisType.X);
    xAxis.setLabel("Month");

    Axis yAxis = barModel.getAxis(AxisType.Y);
    yAxis.setLabel("# of Transactions");

    rs.close();
    ps.close();
    connect.close();

    return officeProductions;
}

public BarChartModel getBarModel() {
    return barModel;
}

// Only one office name.  Using it so I can populate form.
public String getOfficeName() {
    return officeName;
}

public String getOfficeCode() {
    return officeCode;
}

public void setOfficeCode(String officeCode) {
    this.officeCode = officeCode;
}

OfficeProduction Bean

package beans;

import java.text.DecimalFormat;


public class OfficeProduction {
private String total;
private String volume;
private String closingDate;
private String officeName;

public OfficeProduction(String total, String volume, String closingDate, String officeName) {
    this.total = total;
    this.volume = volume;
    this.closingDate = closingDate;
    this.officeName = officeName;
}


public String getTotal() {
    return total;
}

public void setTotal(String total) {
    this.total = total;
}

public String getVolume() {

    // Rather have done this in Presentation layer, but Primefaces is giving me a hard time.
    String volumeCurrency = DecimalFormat.getCurrencyInstance().format(Double.parseDouble(volume));
    return volumeCurrency;
}

public void setVolume(String volume) {
    this.volume = volume;
}

public String getClosingDate() {
    return closingDate;
}

public void setClosingDate(String closingDate) {
    this.closingDate = closingDate;
}

public String getOfficeName() {
    return officeName;
}

public void setOfficeName(String officeName) {
    this.officeName = officeName;
}
}
user3132720
  • 76
  • 1
  • 6
  • what is officeProductions ? it's not attached to a bean? post the whole code so we can look into it further, also are you sure it's not running just an exception in first initialization attempt, and then working the second time? The bean being created is different to data populating into the bean. – VeenarM Mar 11 '16 at 07:05
  • The duplicate answers how to solve this. But as the code shows severe fundamental misunderstandings, here are more links which should be carefully read: http://stackoverflow.com/q/5765853 and http://stackoverflow.com/q/2090033 Furthermore, exception handling is also terrible. Replace the entire catch block by `throw new FacesException(ex)`. Make sure you never continue code flow as if nothing exceptional happened. – BalusC Mar 11 '16 at 08:55
  • @VeenarM I've attached OfficeProduction. – user3132720 Mar 12 '16 at 23:54
  • I'm slowly learning to move from Servlets and jsf and probably not understanding the bean creation. I'm probably even asking the wrong question, my apologies to all. I can see that I can reference the officeName after line. I just want to place text in the header, but I don't think the bean has the value at the time. I'm just not sure what to do. @BalusC TY so much for responding, I'll make those changes. – user3132720 Mar 13 '16 at 00:02

0 Answers0