KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > blandware > atleap > webapp > taglib > core > grid > PrevPageTag


1 /*
2  * Copyright 2004 Blandware (http://www.blandware.com)
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package com.blandware.atleap.webapp.taglib.core.grid;
17
18 import com.blandware.atleap.webapp.taglib.core.BaseHandlerTag;
19 import com.blandware.atleap.webapp.util.core.RequestUtil;
20 import org.apache.struts.taglib.TagUtils;
21
22 import javax.servlet.jsp.JspException JavaDoc;
23 import javax.servlet.jsp.JspTagException JavaDoc;
24 import javax.servlet.jsp.PageContext JavaDoc;
25 import java.io.IOException JavaDoc;
26 import java.io.StringWriter JavaDoc;
27 import java.util.Map JavaDoc;
28
29
30 /**
31  * <p>This tag renders to hyperlink to previous page if it exists. Body of link
32  * will be the body of this tag.
33  * <br />
34  * This tag is only valid when nested within <em>pager</em> tag
35  * </p>
36  * <p><a HREF="PrevPageTag.java.htm"><i>View Source</i></a></p>
37  *
38  * @author Sergey Zubtcovskii <a HREF="mailto:sergey.zubtcovskii@blandware.com">&lt;sergey.zubtcovskii@blandware.com&gt;</a>
39  * @version $Revision: 1.8 $ $Date: 2005/09/21 13:46:00 $
40  * @see FirstPageTag
41  * @see NextPageTag
42  * @see LastPageTag
43  * @jsp.tag name="prevPage"
44  * body-content="scriptless"
45  * @see com.blandware.atleap.webapp.taglib.core.grid.PagerTag
46  */

47 public class PrevPageTag extends BaseHandlerTag {
48
49     /**
50      * Processes the tag
51      *
52      * @throws JspException
53      * @throws IOException
54      */

55     public void doTag() throws JspException JavaDoc, IOException JavaDoc {
56
57         PageContext JavaDoc pageContext = (PageContext JavaDoc) getJspContext();
58
59         PagerTag parentPagerTag = (PagerTag) findAncestorWithClass(this, PagerTag.class);
60
61         if ( parentPagerTag == null ) {
62             JspTagException JavaDoc e = new JspTagException JavaDoc("Parent tag is invalid! This tag is only valid when nested within 'pager' tag");
63             throw e;
64         }
65
66         GridTag parentGridTag = (GridTag) findAncestorWithClass(this, GridTag.class);
67
68         int currentPageNumber = parentGridTag.getCurrentPageNumber().intValue();
69         int firstPageNumber = 1;
70
71         if ( currentPageNumber > firstPageNumber ) {
72
73             String JavaDoc pageUrl = parentGridTag.getPageUrl();
74
75             Map JavaDoc parameterMap = parentGridTag.getParameterMap();
76             parameterMap.put("pageNumber", String.valueOf(currentPageNumber - 1));
77             parameterMap.put("gridName", parentGridTag.getName());
78             pageUrl += "?" + RequestUtil.createQueryStringFromMap(parameterMap, "&");
79
80             StringWriter JavaDoc sw = new StringWriter JavaDoc();
81             StringBuffer JavaDoc sb = sw.getBuffer();
82             sb.append("<a HREF=\"").append(pageUrl).append("\"").append(prepareAttributes()).append(">");
83             getJspBody().invoke(sw);
84             sb.append("</a>");
85             TagUtils.getInstance().write(pageContext, sw.toString());
86         }
87     }
88
89 }
90
Popular Tags