core lib inside of tag file evaluates el incorrectly?
I'm having a major problem getting a tag file to evaluate my el correctly. Here's the test case
test.jsp
======================================
${value==null}
${value==0}
${value!=null and value==0}
<c:if test="${value!=null and value==0}">this is not shown, rightfully so</c:if>
<hr>
<so:test param1="${null}" />
so/test.tag
=========================
<jsp:directive.attribute name="param1"
type="java.lang.Object"
required="true"/>
${param1==null}
${param1==0}
${param1!=null and param1==0}
<c:if test="${param1!=null and param1==0}">why the heck would you show this?</c:if>
And the output of /test.jsp is:
=============================
true
false
false
--
true
false
false
why the heck would you show this?
Does that make any sense to anyone? I don't want the if statement inside the tag file to evaluate to true. Also, if i change the test.jsp to send in a String instead of a null value, i get an exception when it tries to compare it to zero (0) even though i set the type to Object!! Ugg! Need help, thanks in advance,
Kris
Oh, and by the way i've tested this with jakarta's jstl.jar's 1.0 and 1.1, and also glasseye's 1.2 implementation.
# 1
Ok first problem - why the heck is it showing that string?
Answer: your tag file needs to import the jstl tag library.
The tag is a seperate unit from the jsp. If you use the JSTL tag in the tag file, you need to import it.
Right now it would be just treating the <c:if> tag as template text because it doesn't recognise it. You can confirm that by viewing source on the page, - the <c:if> syntax will be visible there.
Next problem: Why an exception when you pass in a String value?
I tried it and got this stack trace:
javax.servlet.jsp.el.ELException: An exception occured trying to convert String "Hello world" to type "java.lang.Long"
org.apache.commons.el.Logger.logError(Logger.java:481)
org.apache.commons.el.Logger.logError(Logger.java:498)
org.apache.commons.el.Logger.logError(Logger.java:566)
org.apache.commons.el.Coercions.coerceToPrimitiveNumber(Coercions.java:440)
org.apache.commons.el.Coercions.applyEqualityOperator(Coercions.java:1049)
org.apache.commons.el.EqualityOperator.apply(EqualityOperator.java:81)
org.apache.commons.el.BinaryOperatorExpression.evaluate(BinaryOperatorExpression.java:170)
org.apache.commons.el.ExpressionEvaluatorImpl.evaluate(ExpressionEvaluatorImpl.java:263)
org.apache.commons.el.ExpressionEvaluatorImpl.evaluate(ExpressionEvaluatorImpl.java:190)
org.apache.jasper.runtime.PageContextImpl.proprietaryEvaluate(PageContextImpl.java:899)
org.apache.jsp.tag.web.testTag2_tag.doTag(testTag2_tag.java:70)
This is caused by the line ${value==0} in your tag code (Binary equality operator, comparing a String and a number failed)
I think that is just in the conversion rules when it tries to compare Strings and numbers. I think its a feature of the EL rather than a bug.
Anyway, I ended up with the following for the tag file:
(not entirely certain of it as I haven't used XML syntax for jsp much, but it seems to work)
<jsp:root
xmlns:jsp="http://java.sun.com/JSP/Page"
xmlns:c="http://java.sun.com/jsp/jstl/core"
version="2.0">
<jsp:directive.attribute name="param1" type="java.lang.Object" required="true"/>
${param1==null}
${param1=='0'}
${param1!=null and param1=='0'}
<c:if test="${param1!=null and param1=='0'}">This will not be shown unless param1 is 0</c:if>
</jsp:root>
Converting 0 to '0' seemed to work.
I had to use single quotes rather than double quotes as it caused a parsing error in the <c:if> tag otherwise.
Cheers,
evnafets