
Question:
I am trying to use a variable inside a custom Struts tag something like follows -
for(String currentMacro : (List<String>)(request.getAttribute("individualMacros"))) {
name = currentMacro.<some-operation>
<html:mce name = "hmtl_<%= name %>" />
Something like this. But <%=name%> is not replaced with the variable value. It works when I am using the variable with a pure HTML tags.
Is there any any way to accomplish this in this case?
Thanks.
Answer1:Use JSP EL (assuming JSP 2.0, and you put "name" into scope). You could also check to the if the TLD allows rtexprs.
<html:mce name="html_${name}"/>
But why use scriptlets? There's rarely (ever?) a good reason.
Answer2:Since we are taking about a custom tag, my guess is that in the <a href="http://java.sun.com/products/jsp/tutorial/TagLibraries9.html" rel="nofollow">TLD</a> file there isn't the rtexprvalue
option set to true for that particular tag attribute:
<attribute>
<name>name</name>
<rtexprvalue>true</rtexprvalue>
.......
</attribute>
The rtexprvalue
specifies that the attribute value may be dynamically evaluated at runtime.
If set to "false" it means that the attribute has a static value which is evaluated at <a href="http://java.sun.com/j2ee/tutorial/1_3-fcs/doc/JSPIntro4.html" rel="nofollow">translation</a>; if set to "true" it means the value can be determined dynamically at runtime. Default is "false".
If the scriptlet does not work, it most likely means rtexprvalue
is false. If you don't have the liberty to change that, then expressions won't work on that particular attribute.