Loading...

taglibs-user@jakarta.apache.org

[Prev] Thread [Next]  |  [Prev] Date [Next]

Re: JSTL 1.0 and the function library Kris Schneider Tue Aug 17 07:37:54 2004

Here's a quick EL-only version of an indexOf tag. There's an example here:

http://www.onjava.com/pub/a/onjava/2002/09/11/jstl2.html

of using Standard taglib classes to build your own EL-aware tags.

package com.dotech;

import java.io.IOException;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspTagException;
import javax.servlet.jsp.PageContext;
import javax.servlet.jsp.tagext.TagSupport;
import org.apache.taglibs.standard.lang.support.ExpressionEvaluatorManager;
import org.apache.taglibs.standard.tag.common.core.Util;

public class IndexOfTag extends TagSupport {

    private String inputEL;
    private String substringEL;
    private String var;
    private int scope;

    public IndexOfTag() {
        super();
        init();
    }

    private void init() {
        this.inputEL = null;
        this.substringEL = null;
        this.var = null;
        this.scope = PageContext.PAGE_SCOPE;
    }

    public void release() {
        init();
        super.release();
    }

    public void setInput(String input) {
        this.inputEL = input;
    }

    public void setSubstring(String substring) {
        this.substringEL = substring;
    }

    public void setVar(String var) {
        this.var = var;
    }

    public void setScope(String scope) {
        this.scope = Util.getScope(scope);
    }

    public int doEndTag() throws JspException {
        String input = (String)ExpressionEvaluatorManager.evaluate("input",
                                                                  
this.inputEL,
                                                                  
String.class,
                                                                   this,
                                                                  
pageContext);
        if (input == null) {
            input = "";
        }
        String substring =
(String)ExpressionEvaluatorManager.evaluate("substring",
                                                                      
this.substringEL,
                                                                      
String.class,
                                                                       this,
                                                                      
pageContext);
        if (substring == null) {
            substring = "";
        }
        int index = input.indexOf(substring);

        if (this.var == null) {
            try {
                pageContext.getOut().print(index);
            } catch (IOException exc) {
                throw new JspTagException(exc.getMessage());
            }
        } else {
            pageContext.setAttribute(var, new Integer(index), scope);
        }

        return EVAL_PAGE;
    }
}

The important part of the TLD would look like:

    <tag>
        <name>indexOf</name>
        <tag-class>com.dotech.IndexOfTag</tag-class>
        <body-content>empty</body-content>
        <attribute>
            <name>var</name>
            <required>false</required>
            <rtexprvalue>false</rtexprvalue>
        </attribute>
        <attribute>
            <name>input</name>
            <required>true</required>
            <rtexprvalue>false</rtexprvalue>
        </attribute>
        <attribute>
            <name>substring</name>
            <required>true</required>
            <rtexprvalue>false</rtexprvalue>
        </attribute>
        <attribute>
            <name>scope</name>
            <required>false</required>
            <rtexprvalue>false</rtexprvalue>
        </attribute>
    </tag>

Quoting Helios Alonso <[EMAIL PROTECTED]>:

> Maybe the simplest thing is to implement that only function in your own tag 
> library (I cannot help very much in this topic but I understand it's not 
> complicated declaring the functions as it's not declaring tags)
> 
> At 14:08 16/08/2004 -0400, you wrote:
> >Because of the difficulty I had integrating Tomcat 5.X with IIS 6, I had 
> >to use Tomcat 4.X instead.  This means no JSP 2.0 and no JSTL 1.1.
> >
> >I had been using the function library for some things, namely trim, 
> >replace, indexOf, and substring.  I am looking for a replacement 
> >library.  The string library from Jakarta can do everything except 
> >indexOf.  Does anyone know of a good function library that I can use?

-- 
Kris Schneider <mailto:[EMAIL PROTECTED]>
D.O.Tech       <http://www.dotech.com/>

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