Tuesday, April 8, 2008

How to add paging to your web page using spring mvc pattern?

How to add paging to your web page using spring mvc pattern?
Its just matter of writing two controllers as you normally do like to others.
But here we have to use the built in functionality of the Spring mvc.
That is the RefreshablePagedListHolder:
(RefreshablePagedListHolder is a PagedListHolder subclass with reloading capabilities. It automatically re-requests the List from the source provider, in case of Locale or filter changes.
Data binding works just like with PagedListHolder. The locale can be specified in Locale's toString syntax, e.g. "locale=en_US". The filter object can be of any custom class, preferably a bean for easy data binding from a request. An instance will simply get passed through to PagedListSourceProvider.loadList. A filter property can be specified via "filter.myFilterProperty")

Now I hope that you are having some idea about what is “RefreshablePagedListHolder is”
So we can now move to game of paging
these are my main Controllers classes

  1. FooReportDisplayController this will load the all the Foo Details to the command view
    by the size of page given

    package nalin.report.paging;
    import org.springframework.validation.BindException;
    import org.springframework.web.servlet.ModelAndView;
    import org.springframework.web.servlet.support.RequestContextUtils;
    import org.springframework.web.servlet.mvc.SimpleFormController;
    import org.springframework.web.bind.ServletRequestDataBinder;
    import org.springframework.beans.support.RefreshablePagedListHolder;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    import java.util.ArrayList;
    import java.util.List;
    /**
    * this will get Foo details from the DB
    * according the the user inputs and will put that
    * details to the page list holder
    */
    public class FooReportDisplayController extends SimpleFormController {
    private FooCommandAndView fooCommandAndView;
    private FooDetailsService fooDetailsService;
    private int nofRecordsperPage;
    private static final String FOOS = "foos";
    private static final String FORCE_REFRESH ="forceRefresh" ;
    private static final String CURRENT_FOOS = "fooList";
    protected ModelAndView onSubmit(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse,
    Object o, BindException e) throws Exception {
    final String fooName = fooCommandAndView.getFooName();
    List fooList;
    fooList = new ArrayList();
    fooList=fooDetailsService.findFooDetails(fooName);
    // this will add the Foo list to the RefreshablePagedListHolder
    // and set the page size that means the number of records per page
    // this will set at the bean
    RefreshablePagedListHolder listHolder = new RefreshablePagedListHolder();
    listHolder.setSource(fooList);
    listHolder.setPageSize(nofRecordsperPage);
    //this will add page list holder by name "foos" to refer anyware
    httpServletRequest.getSession(true).setAttribute(FOOS, listHolder);
    ServletRequestDataBinder binder = new ServletRequestDataBinder(listHolder, FOOS);
    binder.bind(httpServletRequest);
    listHolder.setLocale(RequestContextUtils.getLocale(httpServletRequest));
    boolean forceRefresh = httpServletRequest.getParameter(FORCE_REFRESH) != null;
    listHolder.refresh(forceRefresh);

    //now your page list is in the commandandview you can acces it in anyware
    fooCommandAndView = new FooCommandAndView();
    fooCommandAndView.setListHolder(listHolder);
    return new ModelAndView(getSuccessView(), CURRENT_FOOS, fooCommandAndView);

    }
    public FooCommandAndView getFooCommandAndView() {
    return fooCommandAndView;
    }
    public void setFooCommandAndView(FooCommandAndView fooCommandAndView) {
    this.fooCommandAndView = fooCommandAndView;
    }
    public FooDetailsService getFooDetailsService() {
    return fooDetailsService;
    }
    public void setFooDetailsService(FooDetailsService fooDetailsService) {
    this.fooDetailsService = fooDetailsService;
    }
    public int getNofRecordsperPage() {
    return nofRecordsperPage;
    }
    public void setNofRecordsperPage(int nofRecordsperPage) {
    this.nofRecordsperPage = nofRecordsperPage;

    }

    }

  2. FooReportPagingDisplayController this will load the Foo Details by the page number

    package nalin.report.paging;
    import org.apache.log4j.Logger;
    import org.springframework.beans.support.RefreshablePagedListHolder;
    import org.springframework.web.servlet.ModelAndView;
    import org.springframework.web.servlet.support.RequestContextUtils;
    import org.springframework.web.servlet.mvc.Controller;
    import org.springframework.web.bind.ServletRequestDataBinder;

    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    /**
    * this will provide the facility to
    * load page by given page number
    */
    public class FooReportPagingDisplayController implements Controller {
    private final static Logger logger=Logger.getLogger(FooReportPagingDisplayController.class);
    private static final String FOOS = "foos";
    private static final String FORCE_REFRESH = "forceRefresh";
    private FooCommandAndView fooCommandAndView;
    private static final String CURRENT_FOOS = "fooList";
    private String successView;
    public ModelAndView handleRequest(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse)
    throws Exception {
    if (logger.isInfoEnabled()) {
    logger.info("Creating a RefreshablePagedListHolder ");
    }
    //get the page list holder by providing the name eralier set RefreshablePagedListHolder listHolder = (RefreshablePagedListHolder) httpServletRequest.getSession(true).getAttribute(FOOS);
    if ((listHolder != null)) {
    // apply binder to custom target object
    ServletRequestDataBinder binder = new ServletRequestDataBinder(listHolder, FOOS );
    //Bind the given property values to this binder's target
    binder.bind(httpServletRequest);
    //Set the Locale that the source provider should use for loading the list.listHolder.setLocale(RequestContextUtils.getLocale(httpServletRequest));
    boolean forceRefresh = httpServletRequest.getParameter(FORCE_REFRESH) != null;
    listHolder.refresh(forceRefresh);
    booCommandAndView.setListHolder(listHolder);
    //return the list of foo details to page
    return new ModelAndView(successView, CURRENT_FOOS, fooCommandAndView);
    }
    return null;
    }

    public FooCommandAndView getFooCommandAndView() {
    return fooCommandAndView;
    }

    public void setFooCommandAndView(FooCommandAndView fooCommandAndView) {
    this.fooCommandAndView = fooCommandAndView;
    }

    public String getSuccessView() {
    return successView;
    }
    public void setSuccessView(String successView) {
    this.successView = successView;
    }

    }

No comments: