aspose file tools*
The moose likes JSF and the fly likes paging problem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » JSF
Bookmark "paging problem" Watch "paging problem" New topic
Author

paging problem

Adinath Shirsath
Ranch Hand

Joined: Dec 04, 2007
Posts: 35
Hi,
I want to create jsf page that displays data from database in datatable with paging mechanism. I want to do it with use of Scrollable resultSet. So i have created jsf page in netbeans

check.jsp

<?xml version="1.0" encoding="UTF-8"?>
<!--
Document : check
Created on : Aug 28, 2008, 11:44:06 AM
Author : ashirsa
-->
<jsp:root version="2.1" xmlns:f="http://java.sun.com/jsf/core" xmlns:h="http://java.sun.com/jsf/html" xmlns:jsp="http://java.sun.com/JSP/Page" xmlns:webuijsf="http://www.sun.com/webui/webuijsf">
<jsp:directive.page contentType="text/html;charset=UTF-8" pageEncoding="UTF-8"/>
<f:view>
<webuijsf:page id="page1">
<webuijsf:html id="html1">
<webuijsf:head id="head1">
<webuijsf:link id="link1" url="/resources/stylesheet.css"/>
</webuijsf:head>
<webuijsf:body id="body1" style="-rave-layout: grid">
<webuijsf:form id="form1">
<h:dataTable first="#{Employee.count}" headerClass="list-header" id="dataTable1" rowClasses="list-row-even,list-row-odd"
style="height: 216px; left: 120px; top: 96px; position: absolute" value="#{Employee.result}" rows="5" var="currentRow" width="264">
<h:column id="column1">
<hutputText id="outputText1" value="#{currentRow.emp_id}"/>
<f:facet name="header">
<hutputText id="outputText2" value="Employee Id"/>
</f:facet>
</h:column>
<h:column id="column2">
<hutputText id="outputText3" value="#{currentRow.ename}"/>
<f:facet name="header">
<hutputText id="outputText4" value="Employee Name"/>
</f:facet>
</h:column>
<h:column id="column3">
<hutputText id="outputText5" value="#{currentRow.addr}"/>
<f:facet name="header">
<hutputText id="outputText6" value="Address"/>
</f:facet>
</h:column>
</h:dataTable>

<h:commandLink id="nextLink" value="Next" actionListener="#{Employee.next}"/>
<h:commandLink id="previousLink" value="Previous" actionListener="#{Employee.previous}"/>
</webuijsf:form>
</webuijsf:body>
</webuijsf:html>
</webuijsf:page>
</f:view>
</jsp:root>



I also created banking bean as Employee.java

/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package com.check.beans;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import javax.faces.event.ActionEvent;

/**
*
* @author ashirsa
*/
public class Employee {

private ResultSet result;
private int count;

public int getCount() {
return count;
}

public void setCount(int count) {
this.count = count;
}

public void setResult(ResultSet result) {
this.result = result;
}

public ResultSet getResult() {
if (result == null) {
Connection cn = null;
Statement stmt = null;
String sql = null;
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
cn = DriverManager.getConnection("jdbcdbc:test");
stmt = cn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY);
sql = "select * from employee";
result = stmt.executeQuery(sql);

} catch (Exception ex) {
ex.printStackTrace();
}

}
return result;
}

public void next(ActionEvent ae){
count = count + 5;
}
public void previous(ActionEvent ae){
count = count - 5;
}

/** Creates a new instance of Employee */
public Employee() {
count = 0;
}
}


Now when i run my application. It is working fine when i click on Next link, that is it displays next records from the database. but whenever i click on previous link it gives me exception like

Sep 2, 2008 3:51:04 PM org.apache.catalina.core.ApplicationContext log
SEVERE: null
java.lang.IllegalArgumentException
at javax.faces.model.ResultSetDataModel.setRowIndex(ResultSetDataModel.java:244)
at javax.faces.component.UIData.setRowIndex(UIData.java:418)
at com.sun.faces.renderkit.html_basic.TableRenderer.encodeChildren(TableRenderer.java:270)
at javax.faces.component.UIComponentBase.encodeChildren(UIComponentBase.java:812)
at javax.faces.component.UIComponent.encodeAll(UIComponent.java:886)
at javax.faces.component.UIComponent.encodeAll(UIComponent.java:892)
at javax.faces.component.UIComponent.encodeAll(UIComponent.java:892)
at javax.faces.component.UIComponent.encodeAll(UIComponent.java:892)
at javax.faces.component.UIComponent.encodeAll(UIComponent.java:892)
at javax.faces.component.UIComponent.encodeAll(UIComponent.java:892)
at com.sun.faces.application.ViewHandlerImpl.doRenderView(ViewHandlerImpl.java:245)
at com.sun.faces.application.ViewHandlerImpl.renderView(ViewHandlerImpl.java:176)
at com.sun.rave.web.ui.appbase.faces.ViewHandlerImpl.renderView(ViewHandlerImpl.java:320)
at com.sun.faces.lifecycle.RenderResponsePhase.execute(RenderResponsePhase.java:106)
at com.sun.faces.lifecycle.LifecycleImpl.phase(LifecycleImpl.java:251)
at com.sun.faces.lifecycle.LifecycleImpl.render(LifecycleImpl.java:144)
at javax.faces.webapp.FacesServlet.service(FacesServlet.java:245)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.netbeans.modules.web.monitor.server.MonitorFilter.doFilter(MonitorFilter.java:390)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:175)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:128)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:286)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:844)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:583)
at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:447)
at java.lang.Thread.run(Thread.java:595)
Sep 2, 2008 3:51:04 PM org.apache.catalina.core.StandardWrapperValve invoke
SEVERE: Servlet.service() for servlet Faces Servlet threw exception
java.lang.IllegalArgumentException
at javax.faces.model.ResultSetDataModel.setRowIndex(ResultSetDataModel.java:244)
at javax.faces.component.UIData.setRowIndex(UIData.java:418)
at com.sun.faces.renderkit.html_basic.TableRenderer.encodeChildren(TableRenderer.java:270)
at javax.faces.component.UIComponentBase.encodeChildren(UIComponentBase.java:812)
at javax.faces.component.UIComponent.encodeAll(UIComponent.java:886)
at javax.faces.component.UIComponent.encodeAll(UIComponent.java:892)
at javax.faces.component.UIComponent.encodeAll(UIComponent.java:892)
at javax.faces.component.UIComponent.encodeAll(UIComponent.java:892)
at javax.faces.component.UIComponent.encodeAll(UIComponent.java:892)
at javax.faces.component.UIComponent.encodeAll(UIComponent.java:892)
at com.sun.faces.application.ViewHandlerImpl.doRenderView(ViewHandlerImpl.java:245)
at com.sun.faces.application.ViewHandlerImpl.renderView(ViewHandlerImpl.java:176)
at com.sun.rave.web.ui.appbase.faces.ViewHandlerImpl.renderView(ViewHandlerImpl.java:320)
at com.sun.faces.lifecycle.RenderResponsePhase.execute(RenderResponsePhase.java:106)
at com.sun.faces.lifecycle.LifecycleImpl.phase(LifecycleImpl.java:251)
at com.sun.faces.lifecycle.LifecycleImpl.render(LifecycleImpl.java:144)
at javax.faces.webapp.FacesServlet.service(FacesServlet.java:245)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.netbeans.modules.web.monitor.server.MonitorFilter.doFilter(MonitorFilter.java:390)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:175)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:128)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:286)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:844)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:583)
at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:447)
at java.lang.Thread.run(Thread.java:595)




help me on this.........
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 16140
    
  21

I think unless you set the proper option, a ResultSet can only scroll forward, not backward.


Customer surveys are for companies who didn't pay proper attention to begin with.
Bauke Scholtz
Ranch Hand

Joined: Oct 08, 2006
Posts: 2458
Besides, you should never be passing expensive resources around like that. Always explicitly close the connection, statement and resultset in the same method block as where they are opened.

Create a DAO class with a list() method which takes two parameters: the index of the first row and the row count. Let it invoke a SQL query which returns the desired sublist, map the ResultSet to List<RowObject> and return it. Consult the SQL documentation of the database in question for more details. In for example MySQL you can use the LIMIT function for that.
[ September 02, 2008: Message edited by: Bauke Scholtz ]
Adinath Shirsath
Ranch Hand

Joined: Dec 04, 2007
Posts: 35
Thanks Tim and Bauke.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: paging problem