<分区>
<分区>
我一直在摆弄我的一些教程文件,并尝试将通常只会显示一堆废话的数据表更改为显示花卉信息表格的表格。我认为这些更改会很热门(只是为我放入数据库的任何内容切换变量名称)但似乎我遗漏了一些重要的东西。对此事的一些帮助或指导将不胜感激。
我不断得到的输出不是表格,而是这个(字面意思是这个字符串)
花 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;
}
}
花.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;
}
}
表格样式.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;
}
最佳答案
我遇到了类似的问题。
在创建动态 Web 元素时,我没有在配置中设置 Java Server Faces v2.2 元素。这是在您的元素中下载 java server faces 功能所必需的。检查下图,其中设置以黄色突出显示。根据您使用的版本配置设置。
关于java - JSF 数据表不显示任何数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23297398/