Work at SourceForge, help us to make it a better place! We have an immediate need for a Support Technician in our San Francisco or Denver office.

Close

Javascript Include Tag

2011-01-14
2013-01-31
  • Edgar Dollin
    Edgar Dollin
    2011-01-14

    Code for a what looks like a nice addition.  Rather than directly including the javascript…use a tag.  Will make implementation and documentation easier.

    I have included the TLD change and the source code for the tag.

    Edgar

        <tag>
            <description>
                includes the required javascript
            </description>
            <name>includeJavascript</name>
            <tag-class>net.sourceforge.ajaxtags.tags.AjaxIncludeJavascript</tag-class>
            <body-content>empty</body-content>
            <attribute>
                <description>Directory that the ajaxtags scripts are kept</description>
                <name>directory</name>
                <required>true</required>
                <rtexprvalue>true</rtexprvalue>
            </attribute>
            <attribute>
                <description>Included the prototype library…default is true</description>
                <name>prototype</name>
                <required>false</required>
                <rtexprvalue>true</rtexprvalue>
            </attribute>
            <attribute>
                <description>Included the scriptaculous library…default is true</description>
                <name>scriptaculous</name>
                <required>false</required>
                <rtexprvalue>true</rtexprvalue>
            </attribute>
            <attribute>
                <description>Included the overlibmws library…default is true</description>
                <name>overlibmws</name>
                <required>false</required>
                <rtexprvalue>true</rtexprvalue>
            </attribute>
        </tag>

    package net.sourceforge.ajaxtags.tags;

    import java.io.IOException;

    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.jsp.JspException;
    import javax.servlet.jsp.JspWriter;
    import javax.servlet.jsp.tagext.TagSupport;

    import org.apache.commons.lang.StringUtils;

    public class AjaxIncludeJavascript
       extends TagSupport
    {
    String directory;
    public String getDirectory()
    {
    return directory;
    }
    public void setDirectory( String directory )
    {
    this.directory = directory;
    }

    boolean prototype = true;
    public boolean isPrototype()
    {
    return prototype;
    }
    public void setPrototype( boolean prototype )
    {
    this.prototype = prototype;
    }

    boolean scriptaculous = true;
    public boolean isScriptaculous()
    {
    return scriptaculous;
    }
    public void setScriptaculous( boolean scriptaculous )
    {
    this.scriptaculous = scriptaculous;
    }

    boolean overlibmws = true;
    public boolean isOverlibmws()
    {
    return overlibmws;
    }
    public void setOverlibmws( boolean overlibmws )
    {
    this.overlibmws = overlibmws;
    }

    @Override
    public int doStartTag()
    throws JspException
    {
    HttpServletRequest req_ = (HttpServletRequest) pageContext.getRequest();
    try
    {
    JspWriter out = pageContext.getOut();

    String dir = directory;
    if ( StringUtils.isBlank( dir ) )
    dir = "/ajaxtags";

    String prefix = req_.getContextPath() + dir;

    if ( prototype )
    out.println("<script type='text/javascript' src='" + prefix + "/js/prototype.js'></script>");

    if ( scriptaculous )
    out.println("<script type='text/javascript' src='" + prefix + "/js/scriptaculous/scriptaculous.js'></script>");

    if ( overlibmws )
    out.println("<script type='text/javascript' src='" + prefix + "/js/overlibmws/overlibmws.js'></script>");

    // all of these are required
    out.println("<script type='text/javascript' src='" + prefix + "/js/ajaxtags.js'></script>");
    out.println("<link type='text/css' rel='stylesheet' href='" + prefix + "/css/ajaxtags.css'  >");
    out.println("<link type='text/css' rel='stylesheet' href='" + prefix + "/css/displaytag.css'>");
    }
    catch (IOException ioe)
    {
    ioe.printStackTrace();
    }
    return super.doStartTag();
    }
    }

     
  • This tag should be split into two: ajax:css and ajax:javascript. In one project I have link and script tags on different JSP (Struts tiles), and in another project I've merged all CSS into single minified file. I think another web developers also may attach CSS and JS files differently from topic starter.

     
  • Edgar Dollin
    Edgar Dollin
    2011-01-20

    OK…if I understand you the usage should be

    <ajax:script/> to include the js.
    <ajax:link/> to include the css.

    I'll write with default being all files and exclusion properties.  If there is a possibility of being included, I'll work through the code control to submit properly.

    Thanks