1

我想知道你是否能指出我正确的方向,我有一个搜索方法,它工作正常,但我想显示找到的总结果,我想知道我如何实现这样的东西

public List<Testpaper> Paper(String q)throws SQLException {
    List<Testpaper> test = new ArrayList<Testpaper>();

    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 * from test where testname like ? or subject like ?;"); 
    try {
        ps.setString(1, "%" + q + "%");
        ps.setString(2,"%"+ q + "%");
        ResultSet  result =  ps.executeQuery();        
        while (result.next()) {
            Testpaper testpaper = new Testpaper();
            testpaper.setTestname(result.getString("testname"));
            testpaper.setDescription(result.getString("description"));
            testpaper.setDateuploaded(result.getDate("dateuploaded"));
            testpaper.setLink(result.getString("link"));
            testpaper.setYear(result.getString("year"));
            testpaper.setSubject(result.getString("subject"));
            test.add(testpaper);
        }
    } catch(Exception e1) {

    }
    finally{
        try{
            con.close();
        }
        catch(Exception e2) {            
        }
    }
    return test;
}

JSF 代码

<h:dataTable value="#{search.test}" id="result" var="test"
    rendered="#{not empty search.test}">
    <h:column>
        <a href="#{test.link}" title="Download">
            <h3 style="font-size: medium;font-weight: normal;color: rgb(17, 34, 204);display: block; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; ">
                <h:outputText value="#{test.testname}" />
            </h3>
        </a>
        <h:outputText value="Created by:Ainsley Hanson/Portmore Missionar Prep School"/>
        <p style="line-height: 1.24;direction:rtl;width: 400px;">#{test.description}</p>
        <div >
            <h:outputText value="Test year: #{test.year}"
                style="color:rgb(17, 34, 204);margin-right: 10px;"/>
            <h:outputText value="Date Uploaded: #{test.dateuploaded}"
                style="color:rgb(17, 34,  );margin-right: 10px;">
                <f:convertDateTime pattern="MM.dd.yyyy HH:mm" />
            </h:outputText>                                                                 
            <h:outputText value="Subject :   #{test.subject}"
                style="font-weight:100;color:rgb(17, 34, 204);"/>
        </div>
    </h:column>
</h:dataTable>

我知道它应该显示在数据表之外,但你应该从 mysql.

4

3 回答 3

2

您可以使用 JSTLfn:length()函数来获取List#size()EL 中的值。

<html ... xmlns:fn="http://java.sun.com/jsp/jstl/functions">
...
<p>Total results: #{fn:length(search.test)}</p>
于 2013-06-22T14:05:45.337 回答
1

我更喜欢 facelets 大小方法而不是使用 JSTL:

#{myBean.myList.size()} 之类的东西

于 2013-06-22T18:25:08.323 回答
0

如果您使用的是 EL 2.2,您可以简单地利用新的 EL 规范并像这样打印结果计数:

Results count : #{search.test.size()}
于 2013-06-22T14:19:06.263 回答