2

我有一个带有 java.lang.Integer 类型属性的 .tag 文件,如下所示:

<%@ attribute name="userId" required="true" type="java.lang.Integer"%>

当将一个空字符串传递给此标记时,该属性的值最终为0而不是null(正如我希望使用java.lang.Integer和所有内容一样)。

我错过了什么吗?在这种情况下,有什么方法可以强制属性值为null吗?

4

1 回答 1

0

My guess is that if the type conversion fails (i.e. the value passed in won't parse as an Integer, so Integer.parseInt throws a NumberFormatException) then the attribute is initialised to 0. This kind of makes sense as it distinguishes between that case and a null object reference.

How about having a String attribute instead in your .tag file? You can then test it's value (for null or empty etc) and convert to an Integer as necessary.

于 2012-09-19T20:28:53.047 回答