从jsf查询获取变量sql db在另一个jsf页面显示结果

时间:2016-12-09 18:28:40

标签: java mysql sql eclipse jsf

我试图从第一个jsf页面的行中获取信息,将其传递给dao以查询数据库,然后返回结果以显示在第二个jsf页面上。 问题是,即使查询数据库并返回结果,也不会在第2页的jsf上显示任何内容。

JSF第1页list_vehicle

JSF PAGE 1 list_vehicle

在第一页上 当您单击右侧的“查看”链接时,它将带您到另一个jsf页面,该页面将显示与该车辆相对应的更多详细信息  (即如果我点击第二行,它应该从行中获取该信息。)

将它作为对象传递给controllerm然后传递给DAO,我调用它来获取vech_reg,它查询db并返回一个字符串,然后我将其存储在构造函数中并将其返回给控制器,将其显示回页面2.

我的问题是它从jsf page1到控制器再到dao然后回到控制器但是不会在jsf页面2上显示。页面加载但是只是空白。

我在控制器中有一个打印输出声明,显示正在传回的正确信息,它不会在第2页的jsf上显示。

JSF第2页list_fullvehicle_details.xhtml

enter image description here

list_vehicle.xhtml

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://xmlns.jcp.org/jsf/html"
xmlns:p="http://xmlns.jcp.org/jsf/passthrough"
xmlns:f="http://xmlns.jcp.org/jsf/core">

<h:head>
<title>Vehicle Table</title>


</h:head>

<h:body>

reg : #{controller.v1.vech_reg}


</h:body>
</html>

控制器

import java.sql.SQLException;
import java.util.ArrayList;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.bean.ViewScoped;
//import javax.faces.event.ComponentSystemEvent;

@ManagedBean
@ViewScoped
@SessionScoped
public class Controller {

private ArrayList<Manufacturer> manufacturers;
private ArrayList<Model> models;
private ArrayList<Vehicle> vehicles;
private DAO dao;
private Vehicle v1;


    public void setVehicles(ArrayList<Vehicle> vehicles) {
    this.vehicles = vehicles;
}


public Controller() {
    try {
        dao = new DAO();
    } catch (Exception e) {
        e.printStackTrace();
    }

    public ArrayList<Vehicle> getVehicles() {
        return vehicles;
    }


    public void loadVehicles() throws Exception {
        vehicles = dao.getVehicleDetails();
    }




    public String  loadFullVehicles(Vehicle v) throws SQLException {
         try {
            v1  = dao.getFullVehicleDetails(v);


            System.out.println("controller loadfullVehicles");
            System.out.println("v1 reg: " + v1.getVech_reg());




            return "list_fullvehicle_details";

        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            return e.toString();
        }



    }

    public Vehicle getV1() {
    return v1;
}


public void setV1(Vehicle v1) {
    this.v1 = v1;
}

}

DAO

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

import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;
import javax.naming.*;
import javax.sql.DataSource;

public class DAO {

private DataSource mysqlDS;

public DAO() throws Exception {
    Context context = new InitialContext();
    String jndiName = "java:comp/env/jdbc/garage";
    mysqlDS = (DataSource) context.lookup(jndiName);
}

public ArrayList<Vehicle> getVehicleDetails() throws Exception {

  ArrayList<Vehicle> Vehicles = new ArrayList<>();

    Connection conn = mysqlDS.getConnection();
    PreparedStatement myStmt = conn.prepareStatement("select * from       vehicle;");

    ResultSet rs = myStmt.executeQuery();

    while (rs.next()) {



        String reg = rs.getString("reg");
        String manu_code = rs.getString("manu_code");
        String model_code = rs.getString("model_code");
        int mileage = rs.getInt("mileage");
        Double price = rs.getDouble("price");
        String colour = rs.getString("colour");
        String fuel = rs.getString("fuel");




        Vehicles.add(new Vehicle(reg, manu_code, model_code, mileage, price,colour, fuel ));
    }

    return Vehicles;
}






public Vehicle getFullVehicleDetails(Vehicle v) throws Exception {

    System.out.println("dao loadFullVehiclesDetails");

    Vehicle v1  = null;
    //ArrayList<Vehicle> Vehicles2 = new ArrayList<>();

    Connection conn = mysqlDS.getConnection();


PreparedStatement myStmt = conn.prepareStatement("select v.reg, v.manu_code, m.manu_name, m.manu_details, mo.model_code, mo.model_name, mo.model_desc, v.mileage, v.price, v.colour, v.fuel  from vehicle v left join manufacturer m on v.manu_code = m.manu_code left join model mo on m.manu_code = mo.manu_code where v.reg like '"+ v.getVech_reg() + "';");



    ResultSet rs = myStmt.executeQuery();

    while (rs.next()) {



        String reg = rs.getString("reg");
        String manu_code = rs.getString("manu_code");
        String manu_name = rs.getString("manu_name");
        String manu_details = rs.getString("manu_details");
        String model_code = rs.getString("model_code");
        String model_name = rs.getString("model_name");
        String model_desc = rs.getString("model_desc");
        int mileage = rs.getInt("mileage");
        double price = rs.getDouble("price");
        String colour = rs.getString("colour");
        String fuel = rs.getString("fuel");



        v1 =  new Vehicle(reg, manu_code, manu_name, manu_details, model_code, model_name, model_desc, mileage,  price, colour, fuel);
    }

    return v1;
}

Vehicle.java

import javax.faces.bean.ManagedBean;

@ManagedBean



public class Vehicle {

private String vech_reg;
private String vech_manu_code;
private String vech_model_code;
private int vech_mileage;
private double vech_price;
private String vech_colour;
private String vech_fuel;

private String manu_name;
private String manu_details;
private String model_name;
private String model_desc;

// manu details
//manu name
// model name
// model description






public Vehicle(String vech_reg, String vech_manu_code, String vech_model_code, int vech_mileage,
        double  vech_price,String vech_colour, String vech_fuel) {
    super();
    this.vech_reg = vech_reg;
    this.vech_manu_code = vech_manu_code;
    this.vech_model_code = vech_model_code;
    this.vech_mileage = vech_mileage;
    this.vech_price = vech_price;
    this.vech_colour = vech_colour;
    this.vech_fuel = vech_fuel;
}








public Vehicle(String vech_reg, String vech_manu_code, String manu_name,   String manu_details, 
                String model_code, String model_name, String model_desc, 
                 int mileage, double price, String colour, String fuel) {

    super();
    this.vech_reg = vech_reg;
    this.vech_manu_code = vech_manu_code;
    this.manu_name = manu_name;
    this.manu_details = manu_details;
    this.vech_model_code = model_code;
    this.model_name = model_name;
    this.model_desc = model_desc;
    this.vech_mileage = mileage;
    this.vech_price = price;
    this.vech_colour = colour;
    this.vech_fuel = fuel;



}



public String getVech_reg() {
    return vech_reg;
}

public void setVech_reg(String vech_reg) {
    this.vech_reg = vech_reg;
}

0 个答案:

没有答案