RSS RSS feed | Atom Atom feed
Popular Articles: Tom Riddle's Magical Diary | AJAX Lego Robot | AJAX CAPTCHA | SQL Multisets

DisplayTag Paging

Tip to keep track of which page you're at

In a filter or (in your controller), add something like this
public class DisplayTagPageTracking implements Filter {
    public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException {
        HttpServletRequest request = (HttpServletRequest) req;
        if ("GET".equals(request.getMethod()) && (null != request.getQueryString() &&  request.getQueryString().length()>0)) {
            final String queryString = request.getQueryString();
            final String pageQueryString = DisplayTagUtil.extractPageNameAndValue(queryString);
        chain.doFilter(req, res);
The DisplayTagUtil function looks like this

public class DisplayTagUtil {
    private DisplayTagUtil() {

    private static Pattern extractPagePattern = Pattern.compile("(?sim)(d-\\d+-p=\\d+)");

    public static String extractPageNameAndValue(String queryString) {
        if (null == queryString) {
            return null;
        Matcher m = extractPagePattern.matcher(queryString);
        StringBuilder ret = new StringBuilder();
        int i=0;
        for (;m.find();i++) {
            if (i > 0) {

        if (i==0) {
            return null;
        return ret.toString();
Now, in your  jsp page (or velocity page or what have you), for each link back to a page with a displayTag add the session value for that page like so
<a href="${displaytag_pages}">Bla Bla</a>
That's it!


slashdot digg technorati [more]

Re: DisplayTag Paging

Very nice.

But what if the paged link has other parameters?  (for example, request a particular search). Shouldn't you copy those in too? 


Re: DisplayTag Paging

If you want to get the current page for a particular table you can use the following: int curPage = Integer.parseInt(request.getParameter( (new ParamEncoder("theTableId").encodeParameterName(TableTagParameters.PARAMETER_PAGE)))); Of course, when you define the table you also need to give it the matching id.

Add a comment Send a TrackBack