简体   繁体   English

JSF 未加载数据库内容

[英]JSF Content of Database not loaded

I have an issue with loding content from my database into Jakarta Faces.我在将内容从我的数据库加载到 Jakarta Faces 时遇到问题。

The following Java Code should get the data and load into home.xhtml:以下 Java 代码应该获取数据并加载到 home.xhtml 中:

CarController.java 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汽车.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 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主页.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 class="content">
        <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完整代码可在https://github.com/ArisAccola/carsharing

Hope that somebody can assist me despite the enourmous quantity of code;)尽管代码量很大,但希望有人能帮助我;)

Thanks谢谢

The value of your data.table points to a method which returns nothing.您的 data.table 的值指向一个不返回任何内容的方法。 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.从现在开始,将调用getCars()来获取汽车列表。

This leads to a new problem: carsController hasn't yet loaded the cars.这导致了一个新问题:carsController 还没有加载汽车。 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.loadCars()中不要清除列表。 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).而且,您不应将 bean 放在默认的 scope 中(这是 CDI bean 的“依赖范围”)。 See this answer for details.有关详细信息,请参阅此答案

I hope, that I haven't missed anything.我希望,我没有错过任何东西。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM