首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >JSF数据表不显示任何数据

JSF数据表不显示任何数据
EN

Stack Overflow用户
提问于 2014-04-25 15:36:47
回答 1查看 2K关注 0票数 0

我一直在乱搞一些教程文件,并试图更改一个数据表,通常只会显示一堆胡说八道的表格,显示花卉信息表格。我认为这些更改将是非常热门的(只需为我在数据库中放置的任何内容切换变量名称),但似乎我遗漏了一些重要的东西。如能就此事提供一些帮助或指导,将不胜感激。

我一直得到的输出不是一个表,而是这个(字面意思是这个字符串)。

花ID #{f.flowerID}名称#{f.name}颜色#{f.color} Country #{f.country} Price #{f.price}

与教程中格式良好的表相反。

这是我所有的项目文件。不过,我似乎找不到任何错误日志。

ViewFlowers.xhtml

代码语言:javascript
运行
复制
<?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

代码语言:javascript
运行
复制
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

代码语言:javascript
运行
复制
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

代码语言:javascript
运行
复制
.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;
}
EN

回答 1

Stack Overflow用户

发布于 2019-02-09 07:15:01

我也面临着类似的问题。

在创建动态web项目时,我没有在配置中设置JavaServerFacesv2.2项目。这是下载项目中的java服务器faces功能所必需的。选中下面的图像,设置以黄色突出显示。根据使用的版本配置设置。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/23297398

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档