1

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?

4

1 回答 1

0

看来问题是 ${variableName}。当 jsp 编译 java 类时使用:

pageContext.findAttribute("variableName");

根据 javadocs,findAttribute “按顺序在页面、请求、会话(如果有效)和应用程序范围内搜索命名属性,并返回关联的值或 null。”

由于该变量的值未在标记中传递,因此不会在页面范围内找到它,但随后会在我的班级提供的请求范围内找到它。为了限制能见度,我应该一直在使用

${pageScope.variableName}

改为使用此功能:

pageContext.getAttribute("variableName");

根据 javadocs,getAttribute “返回与页面范围内的名称关联的对象,如果未找到,则返回 null。” 这是期望的行为。仅当作为标签的属性提供时才使用该变量。

函数名称对我来说太相似了。光看名字,你根本不知道两者的区别。

于 2014-07-18T22:07:03.980 回答