DO NOT REPLY TO THIS EMAIL, BUT PLEASE POST YOUR BUG 
RELATED COMMENTS THROUGH THE WEB INTERFACE AVAILABLE AT
<http://nagoya.apache.org/bugzilla/show_bug.cgi?id=22890>.
ANY REPLY MADE TO THIS MESSAGE WILL NOT BE COLLECTED AND 
INSERTED IN THE BUG DATABASE.

http://nagoya.apache.org/bugzilla/show_bug.cgi?id=22890

Enhance html:link and html:javascript tags to avoid servlet context hardcoding

           Summary: Enhance html:link and html:javascript tags to avoid
                    servlet context hardcoding
           Product: Struts
           Version: 1.1 Final
          Platform: Other
        OS/Version: Other
            Status: NEW
          Severity: Normal
          Priority: Other
         Component: Custom Tags
        AssignedTo: [EMAIL PROTECTED]
        ReportedBy: [EMAIL PROTECTED]


There are at least two common cases when servlet context has to be currently 
hardcode.
1. Style sheet link.
2. Java script file include (custom JavaScript functions, validator static 
Javascript.jsp).

I believe that it should not take much to enhance existing (or create new ?)
tags to avoid this problem. In the absence of other solution I've created my 
own tags to work around: app:css, app:js. But I believe that it's such a common 
thing that it would be beneficial to have as a core.

The code for both of them is somewhat trivial. They both take not required 
source attribute and use application property value by default (to fetch the 
default css and js file).

  /**
   * Returns source attribute value prepended by context.
   * Uses property parameter to get the default source value if 
   * source is not specified.
   * 
   * @param messages message resources
   * @param request HttpServletRequest
   * @param property name of the property value of which will be used by default
   * @return source attribute value
   */
  protected String getSourcePath(
    MessageResources messages,
    ServletRequest request,
    String property) {
    String src =
      (getSource() == null
        ? messages.getMessage(property)
        : getSource());

    String context = ((HttpServletRequest)request).getContextPath();

    return (src != null && context != null ? context + src : null);
  }

  /**
   * Renders the style sheet tag.
   *
   * @exception JspException if a JSP exception has occurred
   */
  public int doStartTag() throws JspException {
    HttpSession session = pageContext.getSession();
    ServletRequest request = pageContext.getRequest();
    MessageResources messages =
      (MessageResources)pageContext.getServletContext().getAttribute(
        Globals.MESSAGES_KEY);

    JspWriter out = pageContext.getOut();

    String src = getSourcePath(messages, request, "stylesheet.source");

    if (src == null) {
      throw new JspTagException("Style sheet source attribute is missing.");
    }

    try {
      out.write("<link rel=\"stylesheet\" type=\"text/css\" href=\"");
      out.write(src + "\" />");
      out.newLine();
    }
    catch (IOException e) {
      throw new JspTagException("Failed to write tag attributes: " + e);
    }

    return SKIP_BODY;
  }

  /**
   * Renders the java script tag.
   *
   * @exception JspException if a JSP exception has occurred
   */
  public int doStartTag() throws JspException {
    HttpSession session = pageContext.getSession();
    ServletRequest request = pageContext.getRequest();
    MessageResources messages =
      (MessageResources)pageContext.getServletContext().getAttribute(
        Globals.MESSAGES_KEY);

    JspWriter out = pageContext.getOut();

    String src = getSourcePath(messages, request, "javascript.source");

    if (src == null) {
      throw new JspTagException("Java script source attribute is missing.");
    }

    try {
      out.write("<script language=\"javascript\" src=\"");
      out.write(src + "\"></script>");
      out.newLine();
    }
    catch (IOException e) {
      throw new JspTagException("Failed to write tag attributes: " + e);
    }

    return SKIP_BODY;
  }

---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to