Question

You're making a custom tag library and have defined an attribute:

test.tag:

<%@ attribute name="variableName" required="false" type="java.lang.String" %>
${variableName}

and have a class:

Test.java

public class Test {
     public String getVariableName() {...};
     public List<Test> getTests() {...};
}

and finally a jsp:

<s:iterator value="getTests">
  <t:test />
</s:iterator>

While you have not specified a variableName in the <t:test /> tag, the tag is grabbing a value for variableName from the Test object because it happens to share the same variable name.

How do you avoid this namespace collision?

Was it helpful?

Solution

It seems the issue was ${variableName}. When the jsp compiled the java class uses:

pageContext.findAttribute("variableName");

According to javadocs, findAttribute "Searches for the named attribute in page, request, session (if valid), and application scopes in order and returns the value associated or null."

Since a value for that variable is not passed in the tag, it won't be found in page scope, but it will subsequently be found in request scope as provided by my class. To limit visibility I should have been using

${pageScope.variableName}

Uses this function instead:

pageContext.getAttribute("variableName");

According to javadocs, getAttribute "Returns the object associated with the name in the page scope or null if not found." That is the desired behavior. Use the variable only if supplied as an attribute to the tag.

The function names are too similar for my tastes. By the name alone you wouldn't know the difference between the two.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top