01: package example.taglib;
02:
03: import javax.servlet.jsp.*;
04: import javax.servlet.jsp.tagext.*;
05: import java.io.*;
06:
07: public class MessageTag extends BodyTagSupport {
08:
09: /* tag attributes fields:
10: *
11: * member variables to store the value of tag attributes are treated
12: * as read-only. Resin will set the values using setXXXX() based on
13: * the values passed as attributes to the tag, and the code in the
14: * class will never change the values.
15: */
16: private String _attrTitle;
17:
18: /* internal tag fields
19: *
20: * Since an instance of this class can be reused, initialization of
21: * internal member variables happens in the init() method, which is
22: * called from doStartTag()
23: */
24: private String _title;
25: private StringBuffer _msg;
26:
27: public void setTitle(String title) {
28: // this is the only place where _attrTitle is ever set
29: _attrTitle = title;
30: }
31:
32: public int doStartTag() throws JspException {
33: // initialize internal member variables
34: init();
35:
36: return EVAL_BODY_BUFFERED;
37: }
38:
39: public int doEndTag() throws JspException {
40: // initialize internal member variables
41: init();
42:
43: try {
44: // print the message out
45: JspWriter out = pageContext.getOut();
46: out.println("<p>");
47: out.println("<table border=1>");
48: out.println("<tr><td>");
49: out.println("instance: " + this );
50: out.println("<tr><td>");
51: out.println(_title);
52: out.println("<tr><td>");
53: out.println(_msg.toString());
54: out.println("</table>");
55:
56: } catch (Exception ex) {
57: throw new JspException(ex);
58: }
59:
60: return EVAL_PAGE;
61: }
62:
63: /**
64: * Set defaults for attributes and initialize internal member
65: * variables.
66: */
67: protected void init() {
68: // default value for _title is "Default Title"
69: _title = _attrTitle;
70: if (_title == null)
71: _title = "Default Title";
72:
73: // internal member variables
74: _msg = new StringBuffer();
75:
76: }
77:
78: /**
79: * called by nested children to add to the message. This happens
80: * after doStartTag() and before doEndTag()
81: */
82: void addToMessage(String text) throws JspException {
83: _msg.append(text);
84: _msg.append("<br>");
85: }
86: }
|