【问题标题】:jsf search display total results found in a datatablejsf 搜索显示在数据表中找到的总结果
【发布时间】:2013-06-22 13:39:03
【问题描述】:

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

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 获取结果。

【问题讨论】:

  • 发布您在 JSF 和 Java 代码中尝试过的内容。

标签: java jsf jsf-2


【解决方案1】:

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

<html ... xmlns:fn="http://java.sun.com/jsp/jstl/functions">
...
<p>Total results: #{fn:length(search.test)}</p>

【讨论】:

  • 感谢 BalusC 这正是我想要的
【解决方案2】:

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

类似 #{myBean.myList.size()}

【讨论】:

    【解决方案3】:

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

    Results count : #{search.test.size()}
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-08-12
      • 2015-09-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-09-22
      相关资源
      最近更新 更多