我一直在搞乱我的一些教程文件,并尝试更改一个数据表,通常只会在显示花形信息表的表中显示一堆废话。我认为这些更改将非常热门(只是为我放入数据库中的任何内容切换变量名称)但似乎我错过了一些重要的东西。对此事的一些帮助或指导将不胜感激。
而不是表,我不断得到的输出是这个(字面意思是这个字符串)
Flower ID#{f.flowerID}姓名#{f.name}颜色#{f.color}国家#{f.country}价格#{f.price}
与教程中格式良好的表格相反。
这是我的所有项目文件。我似乎无法找到任何错误日志。
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;
}
}
表的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;
}