Here it is. Most of it is identical to the sample published in the link I've
attached before, searches are done against Compass (great project!) than
against db.
package tests;
import java.io.Serializable;
import java.util.ArrayList;
import javax.faces.component.UICommand;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;
import org.compass.core.CompassDetachedHits;
import org.compass.core.CompassHit;
import org.compass.core.CompassHits;
import tests.store.DataAccessObject;
import tests.store.data.PageMemoryRecord;
public class QueryBean implements Serializable
{
private int
totalRows;
// Paging.
private int
firstRow;
private int
rowsPerPage;
private int
totalPages;
private int
pageRange;
private Integer[] pages;
private int
currentPage;
// Sorting.
private String sortField;
private boolean sortAscending;
public int getTotalRows()
{
return totalRows;
}
public void setTotalRows( int totalRows )
{
this.totalRows = totalRows;
}
public int getFirstRow()
{
return firstRow;
}
public void setFirstRow( int firstRow )
{
this.firstRow = firstRow;
}
public int getRowsPerPage()
{
return rowsPerPage;
}
public void setRowsPerPage( int rowsPerPage )
{
this.rowsPerPage = rowsPerPage;
}
public int getTotalPages()
{
return totalPages;
}
public void setTotalPages( int totalPages )
{
this.totalPages = totalPages;
}
public int getPageRange()
{
return pageRange;
}
public void setPageRange( int pageRange )
{
this.pageRange = pageRange;
}
public Integer[] getPages()
{
return pages;
}
public void setPages( Integer[] pages )
{
this.pages = pages;
}
public int getCurrentPage()
{
return currentPage;
}
public void setCurrentPage( int currentPage )
{
this.currentPage = currentPage;
}
public String getSortField()
{
return sortField;
}
public void setSortField( String sortField )
{
this.sortField = sortField;
}
public boolean isSortAscending()
{
return sortAscending;
}
public void setSortAscending( boolean sortAscending )
{
this.sortAscending = sortAscending;
}
// Paging actions
-----------------------------------------------------------------------------
public void pageFirst()
{
page( 0 );
}
public void pageNext()
{
page( firstRow + rowsPerPage );
}
public void pagePrevious()
{
page( firstRow - rowsPerPage );
}
public void pageLast()
{
page( totalRows - ((totalRows % rowsPerPage != 0) ? totalRows %
rowsPerPage : rowsPerPage) );
}
public void page( ActionEvent event )
{
page( ((Integer) ((UICommand) event.getComponent()).getValue()
- 1) *
rowsPerPage );
}
private void page( int firstRow )
{
this.firstRow = firstRow;
loadDataList(); // Load requested page.
}
// Sorting actions
----------------------------------------------------------------------------
public void sort( ActionEvent event )
{
String sortFieldAttribute = (String)
event.getComponent().getAttributes().get( "sortField" );
// If the same field is sorted, then reverse order, else sort
the new
field ascending.
if( sortField.equals( sortFieldAttribute ) )
{
sortAscending = !sortAscending;
}
else
{
sortField = sortFieldAttribute;
sortAscending = true;
}
pageFirst(); // Go to first page and load requested page.
}
//----------------------------------------------------------------------------------------------------------
/**
*
*/
private static final long serialVersionUID = 1L;
private String email = "";
private String password = "";
private String method = "";
private String query = "";
private ArrayList<MyData> dataList = null;
private CompassDetachedHits hits = null;
public QueryBean()
{
rowsPerPage = 3; // Default rows per page (max amount of rows
to be
displayed at once).
pageRange = 3; // Default page range (max amount of pages links
to be
displayed at once).
sortField = "id"; // Default sort field.
sortAscending = true; // Default sort direction.
}
public String getEmail()
{
return (email);
}
public void setEmail( String email )
{
this.email = email;
}
public String getPassword()
{
return (password);
}
public void setPassword( String password )
{
this.password = password;
}
public String getMethod()
{
return (method);
}
public void setMethod( String method )
{
this.method = method;
}
public String register1()
{
setMethod( "<CODE>commandButton</CODE> with <CODE>value</CODE>"
);
return ("success");
}
public String register2()
{
setMethod( "<CODE>commandButton</CODE> with <CODE>image</CODE>"
);
return ("success");
}
public String register3()
{
setMethod( "<CODE>commandLink</CODE>" );
return ("success");
}
public String register4()
{
PageMemoryRecord record = new PageMemoryRecord();
record.setContent( query );
hits = DataAccessObject.getInstance().searchAsDetach( record );
setTotalRows( hits.length() );
int i=0;
while( compassHitIterator.hasNext() && i<10)
{
PageMemoryRecord result = (PageMemoryRecord)
compassHitIterator.next().getData();
dataList.add( result );
i++;
}*/
pageFirst();
return null;
}
public String register5()
{
dataList = new ArrayList<MyData>();
return null;
}
public ArrayList<MyData> getDataList()
{
PageMemoryRecord record = new PageMemoryRecord();
record.setContent( query );
hits = DataAccessObject.getInstance().searchAsDetach( record );
setTotalRows( hits.length() );
loadDataList();
return dataList;
}
public void setDataList( ArrayList<MyData> dataList )
{
this.dataList = dataList;
}
public String getQuery()
{
return query;
}
public void setQuery( String query )
{
this.query = query;
}
protected void loadDataList()
{
try
{
if( dataList == null )
{
dataList = new ArrayList<MyData>();
}
else
{
dataList.clear();
}
int start = getFirstRow();
//int end = Math.min( getFirstRow() + getRowsPerPage(),
hits.length() );
int end = Math.min( getFirstRow() + getRowsPerPage(),
getTotalRows() );
PageMemoryRecord toSearch = new PageMemoryRecord();
toSearch.setContent( query );
CompassDetachedHits detachedHits =
DataAccessObject.getInstance().searchAsDetach( toSearch, start,
getRowsPerPage() );
for( CompassHit hit : detachedHits )
{
dataList.add( toMyData((PageMemoryRecord)
hit.data() ) );
}
}
catch ( Exception e )
{
throw new RuntimeException( e ); // Handle it yourself.
}
// Set currentPage, totalPages and pages.
currentPage = (totalRows / rowsPerPage) - ((totalRows -
firstRow) /
rowsPerPage) + 1;
totalPages = (totalRows / rowsPerPage) + ((totalRows %
rowsPerPage != 0) ?
1 : 0);
int pagesLength = Math.min( pageRange, totalPages );
pages = new Integer[pagesLength];
// firstPage must be greater than 0 and lesser than
totalPages-pageLength.
int firstPage = Math.min( Math.max( 0, currentPage - (pageRange
/ 2) ),
totalPages - pagesLength );
// Create pages (page numbers for page links).
for( int i = 0; i < pagesLength; i++ )
{
pages[i] = ++firstPage;
}
}
private MyData toMyData( PageMemoryRecord data )
{
MyData my = new MyData();
my.setId( 1L );
my.setName( data.getTitle() );
return my;
}
}
Richard Yee-3 wrote:
>
> Can you send the backing bean code?
>
> -R
>
> On Sun, May 31, 2009 at 1:22 AM, Dvora <[email protected]> wrote:
>>
>> Of course, here is the jsp:
>>
>> <%@ taglib uri="http://java.sun.com/jsf/core" prefix="f"%>
>> <%@ taglib uri="http://java.sun.com/jsf/html" prefix="h"%>
>> <%...@taglib uri="http://myfaces.apache.org/tomahawk" prefix="t"%>
>> <%@ page contentType="text/html; charset=UTF-8"%>
>> <f:view>
>> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
>> <f:loadBundle basename="messages" var="msg" />
>> <HTML>
>> <HEAD>
>> <LINK REL="STYLESHEET" HREF="./css/styles.css" TYPE="text/css">
>> <meta http-equiv="content-type" content="text/html;charset=UTF-8">
>> </HEAD>
>> <BODY>
>> <CENTER>
>> <P><h:form>
>> <P><h:commandButton value="#{msg.bt1}"
>> action="#{queryBean.register4}" />
>> <h:commandButton
>> value="push me empty"
>> action="#{queryBean.register5}" />
>> Query: <h:inputText value="#{queryBean.query}" />
>> <H2>Start data table</H2>
>> <h:dataTable value="#{queryBean.dataList}" var="item"
>> border="1">
>> <h:column>
>> <f:facet name="header">
>> <h:commandLink value="ID"
>> actionListener="#{queryBean.sort}">
>> <f:attribute
>> name="sortField" value="id" />
>> </h:commandLink>
>> </f:facet>
>> <h:outputText value="#{item.id}" />
>> </h:column>
>> <h:column>
>> <f:facet name="header">
>> <h:commandLink value="Name"
>>
>> actionListener="#{queryBean.sort}">
>> <f:attribute
>> name="sortField" value="name" />
>> </h:commandLink>
>> </f:facet>
>> <h:outputText value="#{item.name}" />
>> </h:column>
>> </h:dataTable>
>>
>> <%-- The paging buttons --%>
>> <h:commandButton value="first"
>> action="#{queryBean.pageFirst}"
>> disabled="#{queryBean.firstRow == 0}" />
>> <h:commandButton value="prev"
>> action="#{queryBean.pagePrevious}"
>> disabled="#{queryBean.firstRow == 0}" />
>> <h:commandButton value="next"
>> action="#{queryBean.pageNext}"
>> disabled="#{queryBean.firstRow +
>> queryBean.rowsPerPage >=
>> queryBean.totalRows}" />
>> <h:commandButton value="last"
>> action="#{queryBean.pageLast}"
>> disabled="#{queryBean.firstRow +
>> queryBean.rowsPerPage >=
>> queryBean.totalRows}" />
>> <h:outputText
>> value="Page #{queryBean.currentPage} /
>> #{queryBean.totalPages}" />
>> <br />
>>
>> <%-- The paging links --%>
>> <t:dataList value="#{queryBean.pages}" var="page">
>> <h:commandLink value="#{page}"
>> actionListener="#{queryBean.page}"
>> rendered="#{page !=
>> queryBean.currentPage}" />
>> <h:outputText value="#{page}" escape="false"
>> rendered="#{page ==
>> queryBean.currentPage}" />
>> </t:dataList>
>> <br />
>>
>> <%-- Set rows per page --%>
>> <h:outputLabel for="rowsPerPage" value="Rows per page" />
>> <h:inputText id="rowsPerPage"
>> value="#{queryBean.rowsPerPage}"
>> size="3" maxlength="3" />
>> <h:commandButton value="Set"
>> action="#{queryBean.pageFirst}" />
>> <h:message for="rowsPerPage" errorStyle="color: red;" />
>>
>> <%-- Cache bean with data list, paging and sorting
>> variables for next
>> request --%>
>> <t:saveState value="#{queryBean}" />
>>
>> </h:form>
>> </CENTER>
>> </BODY>
>> </HTML>
>> </f:view>
>>
>> If I populate the text field for the query, then hit the commandButton
>> which
>> associated with the register4() method (poor name, I know, just
>> practicing...), then the query in the backing bean remains empty (the
>> default value) ,and the setter is not invoked.
>>
>> Should I attach the backing bean code as well?
>>
>>
>> Richard Yee-3 wrote:
>>>
>>> Can you send you code? There must be something else going on as a
>>> result of your refactoring because package location of classes does
>>> not matter to JSF.
>>>
>>> Richard
>>>
>>> Sent from my iPhone
>>>
>>> On May 31, 2009, at 12:12 AM, Dvora <[email protected]> wrote:
>>>
>>>>
>>>> Hello all,
>>>>
>>>> I'm new to JSF, and i'm studying mainly by looking code examples.
>>>> One of the
>>>> examples I'm learning now is
>>>> http://balusc.blogspot.com/2008/10/effective-datatable-paging-and-sorting.html
>>>> .
>>>> Everything is working (so far so cool), but, if I refactor the DTO
>>>> MyData to
>>>> be sit in other package than the backing bean, then commandButtons not
>>>> always working (i.e. no data refreshed on the jsf page and
>>>> breakpoints on
>>>> the backing bean are never reached).
>>>> Is this behaviour expected? Where should I tell JSF about other
>>>> packages I'm
>>>> using?
>>>>
>>>> Thanks for any help!
>>>> --
>>>> View this message in context:
>>>> http://www.nabble.com/Packaging-issues-tp23800484p23800484.html
>>>> Sent from the MyFaces - Users mailing list archive at Nabble.com.
>>>>
>>>
>>>
>>
>> --
>> View this message in context:
>> http://www.nabble.com/Packaging-issues-tp23800484p23800926.html
>> Sent from the MyFaces - Users mailing list archive at Nabble.com.
>>
>>
>
>
--
View this message in context:
http://www.nabble.com/Packaging-issues-tp23800484p23801326.html
Sent from the MyFaces - Users mailing list archive at Nabble.com.