Im trying to get information from a row in the first jsf page pass it to the dao to query the database then return the results to be displayed on the 2nd jsf page. The problem is that even though the database is queried and results are returned nothing is displayed on jsf page 2.
JSF PAGE 1 list_vehicle
on the first page when you click on the "View" link on the right it will bring you to another jsf page that will show more details corresponding to that vehicle (i.e if I click on the 2nd row it should take that information from the row.)
pass it as an object to the controllerm then to the DAO where the I call get vech_reg which queries the db and returns a string which I then store in a constructor and return it back to the controller, to display it back onto page 2.
my problem is that it goes from jsf page1 to controller to dao then back to controller but wont display on jsf page 2. the page loads but is just blank.
I have a print out statement in the controller that shows the right information is being passed back it just wont display on jsf page 2.
JSF PAGE 2 list_fullvehicle_details.xhtml
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>
Controller
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;
}