View Javadoc

1   /*
2   * Licensed to the Apache Software Foundation (ASF) under one or more
3   * contributor license agreements.  See the NOTICE file distributed with
4   * this work for additional information regarding copyright ownership.
5   * The ASF licenses this file to You under the Apache License, Version 2.0
6   * (the "License"); you may not use this file except in compliance with
7   * the License.  You may obtain a copy of the License at
8   *
9   *     http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17  package examples;
18  
19  import java.io.IOException;
20  
21  import javax.servlet.jsp.JspException;
22  import javax.servlet.jsp.JspTagException;
23  
24  /**
25   * Example1: the simplest tag
26   * Collect attributes and call into some actions
27   * <p/>
28   * <foo att1="..." att2="...." att3="...." />
29   */
30  
31  public class FooTag
32          extends ExampleTagBase {
33      private String atts[] = new String[3];
34      int i = 0;
35  
36      private final void setAtt(int index, String value) {
37          atts[index] = value;
38      }
39  
40      public void setAtt1(String value) {
41          setAtt(0, value);
42      }
43  
44      public void setAtt2(String value) {
45          setAtt(1, value);
46      }
47  
48      public void setAtt3(String value) {
49          setAtt(2, value);
50      }
51  
52      /**
53       * Process start tag
54       *
55       * @return EVAL_BODY_INCLUDE
56       */
57      public int doStartTag() throws JspException {
58          i = 0;
59          return EVAL_BODY_TAG;
60      }
61  
62      public void doInitBody() throws JspException {
63          pageContext.setAttribute("member", atts[i]);
64          i++;
65      }
66  
67      public int doAfterBody() throws JspException {
68          try {
69              if (i == 3) {
70                  bodyOut.writeOut(bodyOut.getEnclosingWriter());
71                  return SKIP_BODY;
72              } else
73                  pageContext.setAttribute("member", atts[i]);
74              i++;
75              return EVAL_BODY_TAG;
76          } catch (IOException ex) {
77              throw new JspTagException(ex.toString());
78          }
79      }
80  }
81