Evaluate a variable before passing it to the JSP tag handler

When I try to use a custom JSP tag library, I have a variable defined in the JSP that I would like to evaluate before passing it to the tag library. However, I cannot get it to work. Here is a simplified version of my JSP:

<% int index = 8; %>

<foo:myTag myAttribute="something_<%= index %>"/>

doStartTag()My method TagHandleruses the pageContext output stream to write based on the attribute entered:

public int doStartTag() {
    ...
    out.println("Foo: " + this.myAttribute);
}

However, the conclusion that I see in my last markup is as follows:

Foo: something_<%= index %>

instead of what i want:

Foo: something_8

Defining a library tag for an attribute:

<attribute>
    <name>myAttribute</name>
    <required>true</required>
</attribute>

I tried to configure the attribute rtexprvalueboth true, and so false, but did not work. Is there a way I can set up an attribute so that it is evaluated before being sent to the handler? Or do I completely disagree about this?

JSP-, . , , JSP , , .

Edit:

:

<foo:myTag myAttribute="something_${index}"/>

- something_${index}.

+3
3

, <%= ... %> , <%= ... %> - . ?

<% int index = 8; %>
<% String attribValue = "something_" + index; %>

<foo:myTag myAttribute="<%= attribValue %>"/>

EDIT: , <% ... %> . Java.

+6

JSP , , . , :

<foo:myTag >
  <jsp:attribute name="myAttribute">
    something_${index}
  </jsp:attribute>
</foo:myTag >

,

<foo:myTag myAttribute="<%= attribValue %>">
  body
</foo:myTag >

<foo:myTag >
  <jsp:attribute name="myAttribute">
    something_${index}
  </jsp:attribute>
  <jsp:body>
    body
  </jsp:body>
</foo:myTag >
+2

<foo:myTag myAttribute="something_${index}"/>

0

Source: https://habr.com/ru/post/1707081/


All Articles