redochka
redochka

Reputation: 12829

How to expose JSTL custom tag value

The following class is the implementation of a custom tag.

public class TextColorTag extends TagSupport {

    private String var;
    //getters & setters

    public int doStartTag() throws JspException {
         String color = "#eee";
         setValue(var, color);
         JspWriter out = pageContext.getOut();
         out.print(color);
    }

Later in my jsp, when I try to use textColor I find it empty

the color is: <bv:textColor var="textColor" />    <!-- Ok!, display #eee -->

the color is: ${textColor} <!-- Ko!, empty. Why? -->

Of course in the tld I have declared an attribute var.

How to expose the result of the custom tag?

Upvotes: 0

Views: 388

Answers (1)

redochka
redochka

Reputation: 12829

I found the response in javax.servlet.jsp.jstl.core.ConditionalTagSupport

To expose variables, replace the following:

setValue(var, color);

By

pageContext.setAttribute(var, color);

Upvotes: 0

Related Questions