View Javadoc

1   /*
2    * $Id: NestedRootTag.java 376843 2006-02-10 21:02:56Z husted $
3    *
4    * Copyright 1999-2004 The Apache Software Foundation.
5    *
6    * Licensed under the Apache License, Version 2.0 (the "License");
7    * you may not use this file except in compliance with the License.
8    * You may obtain a copy of the License at
9    *
10   *      http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  package org.apache.struts.taglib.nested;
19  
20  import org.apache.struts.taglib.TagUtils;
21  
22  import javax.servlet.http.HttpServletRequest;
23  import javax.servlet.jsp.JspException;
24  import javax.servlet.jsp.tagext.BodyTagSupport;
25  
26  /***
27   * NestedRootTag.
28   *
29   * The only other addition in this nested suite of tags. This tag allows for a
30   * nested structure to start without relying on the bean and workings of the
31   * FormTag. Useful for view pages that don't update when returning to the
32   * server, or use hyperlinks rather than form submits.
33   *
34   * The Bean that it uses can come out of a jsp:useBean tag or define another
35   * bean that's already in scope. As long as the other Struts tags can find the
36   * bean by name, it'll work.
37   *
38   * It's simply recognised by the helper class and it's property is added to
39   * the nesting list.
40   *
41   * @version $Rev: 376843 $ $Date: 2004-10-16 12:38:42 -0400 (Sat, 16 Oct 2004)
42   *          $
43   * @since Struts 1.1
44   */
45  public class NestedRootTag extends BodyTagSupport implements NestedNameSupport {
46      /* usual member variables */
47      private String name = null;
48      private String originalName = "";
49      private String originalNesting = "";
50      private String originalNestingName = "";
51  
52      /***
53       * Getter method for the <i>property</i> property
54       *
55       * @return String value of the property property
56       */
57      public String getProperty() {
58          return "";
59      }
60  
61      /***
62       * Setter method for the <i>property</i> property
63       *
64       * @param property new value for the property property
65       */
66      public void setProperty(String property) {
67      }
68  
69      /***
70       * Getter method for the <i>name</i> property
71       *
72       * @return String value of the name property
73       */
74      public String getName() {
75          return this.name;
76      }
77  
78      /***
79       * Setter method for the <i>name</i> property
80       *
81       * @param name new value for the name property
82       */
83      public void setName(String name) {
84          this.name = name;
85      }
86  
87      /***
88       * Overriding method of the heart of the tag. Gets the relative property
89       * and tells the JSP engine to evaluate its body content.
90       *
91       * @return int JSP continuation directive.
92       */
93      public int doStartTag() throws JspException {
94          /* set the nested reference for possible inclusions etc */
95          HttpServletRequest request =
96              (HttpServletRequest) pageContext.getRequest();
97  
98          // get al the originals
99          originalName = name;
100         originalNesting = NestedPropertyHelper.getCurrentProperty(request);
101         originalNestingName =
102             NestedPropertyHelper.getCurrentName(request, this);
103 
104         // set what we have to
105         if (name != null) {
106             NestedPropertyHelper.setProperty(request, "");
107             NestedPropertyHelper.setName(request, this.name);
108         }
109 
110         // do the JSP thing
111         return (EVAL_BODY_TAG);
112     }
113 
114     /***
115      * Render the resulting content evaluation.
116      *
117      * @return int JSP continuation directive.
118      */
119     public int doAfterBody() throws JspException {
120         /* Render the output */
121         if (bodyContent != null) {
122             TagUtils.getInstance().writePrevious(pageContext,
123                 bodyContent.getString());
124             bodyContent.clearBody();
125         }
126 
127         return (SKIP_BODY);
128     }
129 
130     /***
131      * Evaluate the rest of the page
132      *
133      * @return int JSP continuation directive.
134      */
135     public int doEndTag() throws JspException {
136         /* reset the reference */
137         HttpServletRequest request =
138             (HttpServletRequest) pageContext.getRequest();
139 
140         if (originalNesting == null) {
141             NestedPropertyHelper.deleteReference(request);
142         } else {
143             NestedPropertyHelper.setName(request, originalNestingName);
144             NestedPropertyHelper.setProperty(request, originalNesting);
145         }
146 
147         this.name = originalName;
148 
149         return (EVAL_PAGE);
150     }
151 
152     /***
153      * JSP method to release all resources held by the tag.
154      */
155     public void release() {
156         super.release();
157         this.name = null;
158         this.originalName = null;
159         this.originalNesting = null;
160         this.originalNestingName = null;
161     }
162 }