View Javadoc

1   package org.gnomekr.potron.web.tag;
2   
3   import static org.gnomekr.potron.util.PotronConstants.RESOURCE_NAME;
4   
5   import java.io.IOException;
6   import java.util.MissingResourceException;
7   import java.util.ResourceBundle;
8   
9   import javax.servlet.jsp.JspException;
10  import javax.servlet.jsp.JspWriter;
11  import javax.servlet.jsp.tagext.TagSupport;
12  
13  import org.apache.commons.logging.Log;
14  import org.apache.commons.logging.LogFactory;
15  
16  /***
17   * MessageBoxTag.java
18   * @author Xavier Cho
19   * @version $Revision 1.1 $ $Date: 2005/07/07 15:20:15 $
20   * @jsp:tag 
21   *      name="msgbox"
22   *      body-content="JSP"
23   */
24  public class MessageBoxTag extends TagSupport {
25  
26      private static final long serialVersionUID = 3258407314078839091L;
27  
28      public static final String PARAM_NAME = "msg";
29  
30      private String parameter;
31  
32      /***
33       * @jsp:attribute
34       *      required="false"
35       *      rtexprvalue="true"
36       */
37      public String getParameter() {
38          return parameter;
39      }
40  
41      /***
42       * @param parameter
43       */
44      public void setParameter(String parameter) {
45          this.parameter = parameter;
46      }
47  
48      /***
49       * @see javax.servlet.jsp.tagext.Tag#doStartTag()
50       */
51      public int doStartTag() throws JspException {
52          if (parameter == null) {
53              parameter = PARAM_NAME;
54          }
55  
56          String key = pageContext.getRequest().getParameter(parameter);
57          String message = null;
58  
59          if (key != null) {
60              try {
61                  ResourceBundle resources = ResourceBundle
62                          .getBundle(RESOURCE_NAME);
63  
64                  message = resources.getString(key);
65              } catch (MissingResourceException e) {
66                  Log log = LogFactory.getLog(getClass());
67  
68                  if (log.isWarnEnabled()) {
69                      String msg = "Unable to find the specified message : "
70                              + key;
71                      log.warn(msg, e);
72                  }
73              }
74          }
75  
76          if (message != null) {
77              try {
78                  JspWriter out = pageContext.getOut();
79  
80                  out.print("<script type=\"text/javascript\" ");
81                  out.println("language=\"javascript\">");
82                  out.print("     alert(\"");
83                  out.print(message);
84                  out.println("\");");
85                  out.println("</script>");
86              } catch (IOException e) {
87                  String msg = "Unable to write tag content : " + e.getMessage();
88  
89                  Log log = LogFactory.getLog(getClass());
90                  if (log.isErrorEnabled()) {
91                      log.error(msg, e);
92                  }
93  
94                  throw new JspException(msg);
95              } finally {
96                  release();
97              }
98          } else {
99              release();
100         }
101 
102         return SKIP_BODY;
103     }
104 
105     /***
106      * @see javax.servlet.jsp.tagext.Tag#release()
107      */
108     public void release() {
109         this.parameter = null;
110     }
111 }