这个问题已经在这里有一个答案:
我一直在瞎搞一些教程文件我已经和试图改变这通常只显示了一堆废话成显示花信息形式的表格的数据表。我认为变化将是非常局部的(只是转换变量名字无论我把在数据库中),但它好像我失去了一些东西重要。对此事的一些帮助或指导,将不胜感激。
而不是表,我不断收到输出是这样的(字面此字符串)
花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>
flower bean.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;
}