KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > opensymphony > oscache > web > tag > UseCachedTag


1 /*
2  * Copyright (c) 2002-2003 by OpenSymphony
3  * All rights reserved.
4  */

5 package com.opensymphony.oscache.web.tag;
6
7 import javax.servlet.jsp.JspTagException JavaDoc;
8 import javax.servlet.jsp.tagext.TagSupport JavaDoc;
9
10 /**
11  * UseCachedTag is a tag that tells a &lt;cache&gt; tag to reuse the cached body.<p>
12  *
13  * Usage Example:
14  * <pre><code>
15  * &lt;%@ taglib uri="oscache" prefix="cache" %&gt;
16  * &lt;cache:cache key="mycache" scope="application"&gt;
17  * if (reuse cached)
18  * &lt;cache:usecached /&gt;
19  * else
20  * some other logic
21  * &lt;/cache:cache&gt;
22  * </code></pre>
23  *
24  * Note this is very useful with try / catch blocks
25  * so that you can still produce old cached data if an
26  * exception occurs, eg your database goes down.<p>
27  *
28  * @author <a HREF="mailto:mike@atlassian.com">Mike Cannon-Brookes</a>
29  * @version $Revision: 1.1 $
30  */

31 public class UseCachedTag extends TagSupport JavaDoc {
32     boolean use = true;
33
34     /**
35      * Set the decision to use the body content of the ancestor &lt;cache&gt; or not.
36      *
37      * @param value Whether or not to use the body content. Default is true.
38      */

39     public void setUse(boolean value) {
40         this.use = value;
41     }
42
43     /**
44      * The start tag.
45      *
46      * @throws JspTagException The standard tag exception thrown.
47      * @return The standard Tag return.
48      */

49     public int doStartTag() throws JspTagException JavaDoc {
50         CacheTag cacheTag = (CacheTag) TagSupport.findAncestorWithClass(this, CacheTag.class);
51
52         if (cacheTag == null) {
53             throw new JspTagException JavaDoc("A UseCached tag must be nested within a Cache tag");
54         }
55
56         cacheTag.setUseBody(!use);
57
58         return SKIP_BODY;
59     }
60 }
61
Popular Tags