ibatis-user-java mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Brett Gorres <bgor...@yahoo.com>
Subject Re: RES: PaginatedList & 'google like' results
Date Fri, 18 Mar 2005 15:53:44 GMT
Leonardo:

I succeeeded using only 1 query with PaginatedList.

Btw unless I'm mistaken PaginatedList is tightly 
coupled to iBATIS but that's a sacrifice of
portability one can make for the sake of convenient
paging.  If you switch from iBATIS, yes you can
revisit this dependency and insert different code for
that.

For this example code:  I had decided at the last
minute to add a "jumping" feature to an intranet
search results page that may be similar to what you
need.

It is aware of only 5 possible fixed jumps.  

I allowed specific jumps within results, like so:  
<First> | <25%> | <Middle> | <75%> | <Last>

The JSP knows when to enable or disable the first /
last links.  I know I added 25%, middle and 75% to
what was done in JPetStore, among other things.  I
don't remember how much of this overlaps with that
example.

Brice's parameterized approach may make more sense,
depending on what you need.  (I wanted fixed
navigation options. I think Brice and JPetStore are
both using a more relative approach.)

This worked for me with a ton of search results--YMMV.
   If you do have only a few results, maybe the
arithmetic could be made to be more precise.


(Snippets from my Action and some Struts/JSP example
code pasted in below):


// Any feedback on this is appreciated! -Brett Gorres
// www.clevergreen.net
    public String doPreviousPage()
    {
        if (this.getPaginatedPatientList() != null
            &&
this.getPaginatedPatientList().isPreviousPageAvailable())
        {
            this.paginatedPatientList.previousPage();
            return "success";
        }
        else
        {
           
ActionContext.getActionContext().addSimpleError(
                "There were no previous results to
display.");
            return "failure";
        }
    }

    public String doNextPage()
    {
        if (this.getPaginatedPatientList() != null
            &&
this.getPaginatedPatientList().isNextPageAvailable())
        {
            this.getPaginatedPatientList().nextPage();
            return "success";
        }
        else
        {
           
ActionContext.getActionContext().addSimpleError(
                "There were no more results to
display.");
            return "failure";
        }
    }

    public String doFirstPage()
    {
        if (this.getPaginatedPatientList() != null)
        {
           
this.getPaginatedPatientList().gotoPage(0);
            return "success";
        }
        else
        {
           
ActionContext.getActionContext().addSimpleError(
                "Unable to change pages as
requested.");
            return "failure";
        }
    }

    public String doLastPage()
    {
        if (this.getPaginatedPatientList() != null)
        {
           
this.getPaginatedPatientList().gotoPage(_countPages());
            return "success";
        }
        else
        {
           
ActionContext.getActionContext().addSimpleError(
                "Unable to change pages as
requested.");
            return "failure";
        }
    }

    public String doJump25Percent()
    {
        if (this.getPaginatedPatientList() != null)
        {
           
this.getPaginatedPatientList().gotoPage(_countPages()
/ 4);
            return "success";
        }
        else
        {
           
ActionContext.getActionContext().addSimpleError(
                "Unable to change pages as
requested.");
            return "failure";
        }
    }

    public String doJump50Percent()
    {
        if (this.getPaginatedPatientList() != null)
        {
           
this.getPaginatedPatientList().gotoPage(_countPages()
/ 2);
            return "success";
        }
        else
        {
           
ActionContext.getActionContext().addSimpleError(
                "Unable to change pages as
requested.");
            return "failure";
        }
    }

    public String doJump75Percent()
    {
        if (this.getPaginatedPatientList() != null)
        {
            this.getPaginatedPatientList().gotoPage(3
* _countPages() / 4);
            return "success";
        }
        else
        {
           
ActionContext.getActionContext().addSimpleError(
                "Unable to change pages as
requested.");
            return "failure";
        }
    }

    private int _countPages()
    {
        int pageCount = 0;
        if (this.getPaginatedPatientList() != null
            && this.countMatchingResults != null
            && this.countMatchingResults.intValue() >
0)
        {
            pageCount =
                this.countMatchingResults.intValue()
                    /
AppConstantsInHere.PAGINATED_LIST_PAGE_SIZE;
        }
        return pageCount;
    }


---------------------------------------
Using Struts/iBATIS PaginatedList 
for absolute result Navigation in HTML:
---------------------------------------


      <logic:equal name="myBean"
property="paginatedList.previousPageAvailable"
value="true">
      <html:link
page="/firstPage.do">&lt;&lt;First</html:link>
      </logic:equal>
      <logic:notEqual name="myBean"
property="paginatedList.previousPageAvailable"
value="true">
      <span class="disabledLink">&lt;&lt;First</span>
      </logic:notEqual>
      &nbsp; | &nbsp;  
      <html:link
page="/jumpToTwentyFivePercent.do">25%</html:link>
&nbsp; | &nbsp; 
      <html:link page="/jumpToMiddlePage.do">Middle
Page</html:link> &nbsp; | &nbsp; 
      <html:link
page="/jumpToSeventyFivePercent.do">75%</html:link>
      &nbsp; | &nbsp;
      <logic:equal name="myBean"
property="paginatedList.nextPageAvailable"
value="true">
      <html:link page="/lastPage.do">Last
&gt;&gt;</html:link></div>
      </logic:equal>
      <logic:notEqual name="myBean"
property="paginatedList.nextPageAvailable"
value="true">
      <span class="disabledLink">Last &gt;&gt;</span>
      </logic:notEqual>

-Brett

--- Leonardo Holanda <leo@atlantico.com.br> wrote:
> That's nice. The problem in my case is that the
> Architects in my company
> does not allow us to put database layer especific
> code into the web
> layer code. But, if it's not a problem, thats nice.
> But remember, if one
> day you decide to chance iBatis for another
> framework, you will have to
> touch the web code (the action in your example).
> 
> -----Mensagem original-----
> De: Brice Ruth [mailto:bdruth@gmail.com] 
> Enviada em: sexta-feira, 18 de março de 2005 11:25
> Para: ibatis-user-java@incubator.apache.org
> Assunto: Re: PaginatedList & 'google like' results
> 
> 
> The product catalogs on www.powersentry.com,
> www.newpoint.com,
> www.zincklysbro.dk, and www.wilkinsonswordgarden.com
> all use
> PaginatedList to provide the Page [1] 2 3 4 ...
> functionality.
> 
> I have a simple Action that looks like this:
> 
> 	public ActionForward gotoPage(
> 		ActionMapping mapping,
> 		ActionForm form,
> 		HttpServletRequest request,
> 		HttpServletResponse response) 
> 	{
> 		DynaActionForm myForm = (DynaActionForm) form;
> 		// Get PRODUCT_INFO_KEY object from session
> 		PaginatedList products = (PaginatedList)
>
request.getSession().getAttribute(Constants.PRODUCT_LIST_KEY);
> 		
> 		if (products != null) {
> 			try {
> 				String page = (String)
> myForm.get("page");
> 				if (page.equalsIgnoreCase("next")) {
> 					products.nextPage();
> 				} else if
> (page.equalsIgnoreCase("previous")) {
> 					products.previousPage();
> 				} else {
> 					int pageNum = new
> Integer(page).intValue();
> 				
> 					products.gotoPage(pageNum);
> 				}
> 				
> 	
>
request.setAttribute(Constants.PRODUCT_LIST_CURRENT_PAGE,new
> Integer(products.getPageIndex()));
> 				
> 				return
> mapping.findForward("success.showCategory");
> 				
> 			} catch (NumberFormatException e) {
> 				return mapping.findForward("browse");
> 			} catch (RuntimeException e) {
> 				return mapping.findForward("browse");
> 			}
> 		} else {
> 			return mapping.findForward("browse");
> 		}
> 	}
> 
> To make this work, when I do the initial query to
> populate the
> PaginatedList, I do as you indicated - one query for
> the objects, one
> query for the count. To hide this double-step, you
> could put that in
> your DAO, if you're using one (I am not).
> 
> Brice
> 
> 
> On Fri, 18 Mar 2005 12:12:31 +0100, Guido García
> Bernardo
> <ggb275@tid.es> wrote:
> > Hi,
> > 
> > I am using ibatis PaginatedList functionality for
> paging through 
> > search results, and I would like to add the
> ability for a user to go 
> > to the last page of the results (something like
> Google results) 
> > instead of having to page through big result sets
> as it is done in 
> > JPetStore4:
> > 
> >   <logic:notEqual name="orderBean"
> property="orderList.firstPage"
> value="true" >
> >     <a
>
href="switchOrderPage.shtml?pageDirection=previous">PREV</a>
> >   </logic:notEqual>
> >   <logic:notEqual name="orderBean"
> property="orderList.lastPage"
> value="true" >
> >     <a
>
href="switchOrderPage.shtml?pageDirection=next">NEXT</a>
> >   </logic:notEqual>
> > 
> > Is this possible or should I do two queries (my
> actual "select fields 
> > from..." and a new "select count(*) from...") to
> achieve it?
> > 
> > Thank you very much,
> > Guido.
> > 
> > --
> > Guido García Bernardo - ggarciab@itdeusto.com
> > Tfn. +34 983 54 89 08
> > ITDEUSTO - Valladolid
> > 
> >
> 
> 

Mime
View raw message