I have been messing around with some tutorial files I had and tried changing a data table that would normally just display a bunch of nonsense into a table that displays flower information form. I thought the changes would be pretty topical (just switching variable names for whatever I put in the database) but it seems as though I am missing something important. Some help or direction on the matter would be appreciated.
Instead of a table, the output I keep getting is this (literally this string)
Flower ID #{f.flowerID} Name #{f.name} Color #{f.color} Country #{f.country} Price #{f.price}
as opposed to the nicely formatted table from the tutorial.
Here are all my project files. I can't seem to find any error logs though.
ViewFlowers.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:f="http://xmlns.jcp.org/jsf/core">
<h:head>
<title>Flowers ABOUND</title>
<h:outputStylesheet library="css" name="table-style.css" />
</h:head>
<h:body>
<h1>FLOWERS GALORE</h1>
<h:dataTable value="#{flower.getFlowerList()}" var="f"
styleClass="order-table"
headerClass="order-table-header"
rowClasses="order-table-odd-row,order-table-even-row"
>
<h:column>
<f:facet name="header">
Flower ID
</f:facet>
#{f.flowerID}
</h:column>
<h:column>
<f:facet name="header">
Name
</f:facet>
#{f.name}
</h:column>
<h:column>
<f:facet name="header">
Color
</f:facet>
#{f.color}
</h:column>
<h:column>
<f:facet name="header">
Country
</f:facet>
#{f.country}
</h:column>
<h:column>
<f:facet name="header">
Price
</f:facet>
#{f.price}
</h:column>
</h:dataTable>
</h:body>
</html>
FlowerBean.java
import jsf.Flower;
@ManagedBean(name="flower")
@RequestScoped
public class FlowerBean implements Serializable{
/**
* Creates a new instance of FlowerBean
*/
DataSource ds;
public FlowerBean() {
//resource injection
// @Resource(name="jdbc/flower")
// if resource injection is not support, you still can get it manually.
try {
Context ctx = new InitialContext();
ds = (DataSource)ctx.lookup("jdbc:mysql://localhost/flow");
} catch (NamingException e) {
e.printStackTrace();
}
}
//connect to DB and get customer list
public List<Flower> getFlowerList() throws SQLException{
if(ds==null)
throw new SQLException("Can't get data source");
//get database connection
Connection con = ds.getConnection();
if(con==null)
throw new SQLException("Can't get database connection");
PreparedStatement ps
= con.prepareStatement(
"select flower_id, flower_name, flower_color, "
+ "flower_country, flower_price from customer");
//get customer data from database
ResultSet result = ps.executeQuery();
List<Flower> list = new ArrayList<Flower>();
while(result.next()){
Flower flow = new Flower();
flow.setFlowerID(result.getLong("flower_flowerid"));
flow.setName(result.getString("flower_name"));
flow.setColor(result.getString("flower_color"));
flow.setCountry(result.getString("flower_country"));
flow.setPrice(result.getDouble("flower_price"));
//store all data into a List
list.add(flow);
}
return list;
}
}
Flower.java
package jsf;
public class Flower {
public long flowerID;
public String name;
public String color;
public String country;
public double price;
public long getFlowerID() {
return flowerID;
}
public void setFlowerID(long flowerID) {
this.flowerID = flowerID;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
public String getCountry() {
return country;
}
public void setCountry(String country) {
this.country = country;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
}
table-style.css
.order-table{
border-collapse:collapse;
}
.order-table-header{
text-align:center;
background:none repeat scroll 0 0 #E5E5E5;
border-bottom:1px solid #BBBBBB;
padding:16px;
}
.order-table-odd-row{
text-align:center;
background:none repeat scroll 0 0 #FFFFFFF;
border-top:1px solid #BBBBBB;
}
.order-table-even-row{
text-align:center;
background:none repeat scroll 0 0 #F9F9F9;
border-top:1px solid #BBBBBB;
}