Home > Blockchain >  JSF Content of Database not loaded
JSF Content of Database not loaded

Time:11-20

I have an issue with loding content from my database into Jakarta Faces.

The following Java Code should get the data and load into home.xhtml:

CarController.java

package com.ffhs.carsharing_v2.controllers;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import com.ffhs.carsharing_v2.helpers.CarsHelper;
import com.ffhs.carsharing_v2.pojos.Cars;

import jakarta.faces.application.FacesMessage;
import jakarta.faces.context.FacesContext;
import jakarta.inject.Named;


@Named
public class CarController implements Serializable {

    private static final long serialVersionUID = 1L;
    private List<Cars> cars;
    private CarsHelper carsHelper;

    public CarController() throws Exception {
        cars = new ArrayList<Cars>();
        carsHelper = CarsHelper.getInstance();
    }

    public List<Cars> getCars() {
        return cars;
    }

    public void loadCars () {
        cars.clear();
        try {
            cars = carsHelper.getCars();
        }catch (Exception e) {
            addErrorMessage (e);
        }
    }

    private void addErrorMessage(Exception ex) {
        FacesMessage message = new FacesMessage(ex.getMessage());
        FacesContext.getCurrentInstance().addMessage(null, message);
    }
}

CarsHelper

package com.ffhs.carsharing_v2.helpers;

import com.ffhs.carsharing_v2.pojos.Cars;
import com.ffhs.carsharing_v2.utilities.DataConnection;


import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

public class CarsHelper {
    private static CarsHelper instance;
       
    public static CarsHelper getInstance() throws Exception {
        if (instance == null) {
            instance = new CarsHelper();
        }
        return instance;
    }
  
    public List<Cars> getCars() throws Exception {
        List<Cars> cars = new ArrayList<>();
        Connection connection = null;
        PreparedStatement carsStatement;

        try {
            connection = DataConnection.getConnection();
            carsStatement = connection.prepareStatement("select * from cars");
                        ResultSet rs = carsStatement.executeQuery();

            while(rs.next()) {
                String carManufacturer = rs.getString("carManufacturer");
                String carModel = rs.getString("carModel");
                String carType = rs.getString("carType");
                String plateNumber = rs.getString("plateNumber");
                String status = rs.getString("status");

                Cars car = new Cars(carManufacturer, carModel,carType,plateNumber,status);
                cars.add(car);
            }
            return cars;
        }
        catch(Exception e) {
            System.out.println(e.getClass().getName()   ": "   e.getMessage());
            return null;
        }
        finally {
            DataConnection.close(connection);
        }
    }
}

Cars.java

package com.ffhs.carsharing_v2.pojos;

public class Cars {

   private String carManufacturer;

    private String carModel;

    private String carType;

    private String plateNumber;

    private String status;

    public Cars(String carManufacturer, String carModel, String carType, String plateNumber, String status) {
        this.carManufacturer = carManufacturer;
        this.carModel = carModel;
        this.carType = carType;
        this.plateNumber = plateNumber;
        this.status = status;
    }

    public String getCarManufacturer(){
        return carManufacturer;
    }

    public void setCarManufacturer(){
        this.carManufacturer = carManufacturer;
    }


    public String getCarModel() {
        return carModel;
    }

    public void setCarModel(String carModel) {
        this.carModel = carModel;
    }

    public String getCarType() {
        return carType;
    }

    public void setCarType(String carType) {
        this.carType = carType;
    }

    public String getPlateNumber() {
        return plateNumber;
    }

    public void setPlateNumber(String plateNumber) {
        this.plateNumber = plateNumber;
    }

    public String getStatus() {
        return status;
    }

    public void setStatus(String status) {
        this.status = status;
    }
}

DataConnection.java

package com.ffhs.carsharing_v2.utilities;

import java.sql.*;
public class DataConnection
{
    public static Connection getConnection()
    {
        try {
            // Set up connection
            Class.forName("com.mysql.jdbc.Driver");
            Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/carsharing", "root", "root");
            return connection;
        } catch (Exception e) {
            System.out.println(e.getClass().getName()   ": "   e.getMessage());
            return null;
        }
    }

    public static void close(Connection connection)
    {
        try {
            connection.close();
        } catch (Exception e) {

        }
    }
}

home.xhtml

<?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:h="http://xmlns.jcp.org/jsf/html"
      xmlns:ui="http://xmlns.jcp.org/jsf/facelets"
      xmlns:f="http://xmlns.jcp.org/jsf/core">

<h:head>
    <title>Car Reservation System - Home</title>
    <h:outputStylesheet name="css/footer.css" />
    <h:outputStylesheet name="css/home.css" />
    <h:outputStylesheet name="css/header.css" />
    <h:outputStylesheet library="webjars" name="font-awesome/6.2.0/css/all.min-jsf.css" />
</h:head>
<h:body>
    <ui:insert name="header">
    <ui:include src="templates/commonHeader.xhtml"></ui:include>
    </ui:insert>

    <div >
        <h1> Welcome #{login.username}</h1>

        <div>
            <h2>Employees List</h2>
            <hr/>
            <h:dataTable value="#{carController.loadCars()}" var="car" border="1">

                <h:column>

                    <f:facet name="header">Manufacturer</f:facet>
                    #{car.carManufacturer}

                </h:column>
                <h:column>

                    <f:facet name="header">Model</f:facet>
                    #{car.carModel}

                </h:column>
                <h:column>

                    <f:facet name="header">Type</f:facet>
                    #{car.carType}

                </h:column>
                <h:column>

                    <f:facet name="header">Plate Number</f:facet>
                    #{car.plateNumber}

                </h:column>
                <h:column>

                    <f:facet name="header">Status</f:facet>
                    #{car.status}

                </h:column>
            </h:dataTable>

            <h:messages />
        </div>
    </div>




    <ui:insert name="footer">
        <ui:include src="templates/commonFooter.xhtml"></ui:include>
    </ui:insert>
</h:body>

</html>

The full code is available on https://github.com/ArisAccola/carsharing

Hope that somebody can assist me despite the enourmous quantity of code ;)

Thanks

CodePudding user response:

The value of your data-table points to a method which returns nothing. Change the value to

<h:dataTable value="#{carController.cars}" var="car" border="1">

From now on, getCars() will be called to get the list of cars.

This leads to a new problem: carsController hasn't yet loaded the cars. You can do this from within your constructor, but it's better to use a lifecycle method:

@PostConstruct
public void setup() {
    if (cars == null) {
        loadCars();
    }
}

In loadCars() don't clear the list. This is not neccessary as you replace the whole list:

public void loadCars () {
    try {
        cars = carsHelper.getCars();
    }catch (Exception e) {
        addErrorMessage (e);
    }
}

And, you should not place your bean in the default scope (that's the 'dependent scope' for CDI beans). See this answer for details.

I hope, that I haven't missed anything.

  • Related