Bug 56029 - Ternary operator doesn't work as expected inside attributes in jspx pages
Ternary operator doesn't work as expected inside attributes in jspx pages
Status: RESOLVED FIXED
Product: Tomcat 6
Classification: Unclassified
Component: Jasper
unspecified
All Mac OS X 10.4
: P2 major (vote)
: default
Assigned To: Tomcat Developers Mailing List
:
: 56031 56124 (view as bug list)
Depends on:
Blocks:
  Show dependency tree
 
Reported: 2014-01-18 12:42 UTC by Michael Simons
Modified: 2014-02-10 10:18 UTC (History)
2 users (show)



Attachments
test.jspx - Failure to trim leading space from fn prefix (426 bytes, text/plain)
2014-01-20 12:10 UTC, Konstantin Kolinko
Details

Note You need to log in before you can comment on or make changes to this bug.
Description Michael Simons 2014-01-18 12:42:47 UTC
The following JSPX file worked upto Tomcat 7.0.47:

<?xml version="1.0" encoding="UTF-8"?>
<jsp:root xmlns:jsp="http://java.sun.com/JSP/Page" version="2.1"
	xmlns:c="http://java.sun.com/jsp/jstl/core" 
	xmlns:fmt="http://java.sun.com/jsp/jstl/fmt"	
>
	<jsp:directive.page contentType="text/html; charset=utf-8"  pageEncoding="UTF-8" />
	<jsp:directive.page session="false" />
	<jsp:output omit-xml-declaration="true" />
	<div>	
		<span id="mainNavNews" class="${currentController eq 'News' ? 'selectedItem' : ''}">foobar</span>	
	</div>	
</jsp:root>

With tomcat 7.0.50 it fails with the following exception:

org.apache.jasper.JasperException: /WEB-INF/views/layouts/mainNavigation.jspx (line: 10, column: 87) "${currentController eq 'News' ? 'selectedItem' : ''}" contains invalid expression(s): javax.el.ELException: Failed to parse the expression [${currentControllereq'News'?'selectedItem':''}]
	at org.apache.jasper.compiler.DefaultErrorHandler.jspError(DefaultErrorHandler.java:42)
	at org.apache.jasper.compiler.ErrorDispatcher.dispatch(ErrorDispatcher.java:443)
	at org.apache.jasper.compiler.ErrorDispatcher.jspError(ErrorDispatcher.java:199)
	at org.apache.jasper.compiler.Validator$ValidateVisitor.getJspAttribute(Validator.java:1399)
	at org.apache.jasper.compiler.Validator$ValidateVisitor.visit(Validator.java:772)
	at org.apache.jasper.compiler.Node$UninterpretedTag.accept(Node.java:1251)
	at org.apache.jasper.compiler.Node$Nodes.visit(Node.java:2375)
	at org.apache.jasper.compiler.Node$Visitor.visitBody(Node.java:2427)
	at org.apache.jasper.compiler.Validator$ValidateVisitor.visit(Validator.java:779)
	at org.apache.jasper.compiler.Node$UninterpretedTag.accept(Node.java:1251)
	at org.apache.jasper.compiler.Node$Nodes.visit(Node.java:2375)
	at org.apache.jasper.compiler.Node$Visitor.visitBody(Node.java:2427)
	at org.apache.jasper.compiler.Validator$ValidateVisitor.visit(Validator.java:529)
	at org.apache.jasper.compiler.Node$JspRoot.accept(Node.java:564)
	at org.apache.jasper.compiler.Node$Nodes.visit(Node.java:2375)
	at org.apache.jasper.compiler.Node$Visitor.visitBody(Node.java:2427)
	at org.apache.jasper.compiler.Node$Visitor.visit(Node.java:2433)
	at org.apache.jasper.compiler.Node$Root.accept(Node.java:474)
	at org.apache.jasper.compiler.Node$Nodes.visit(Node.java:2375)
	at org.apache.jasper.compiler.Validator.validateExDirectives(Validator.java:1817)
	at org.apache.jasper.compiler.Compiler.generateJava(Compiler.java:217)
	at org.apache.jasper.compiler.Compiler.__compile(Compiler.java:373)
	at org.apache.jasper.compiler.Compiler.compile(Compiler.java)
	at org.apache.jasper.compiler.Compiler.compile(Compiler.java:353)
	at org.apache.jasper.compiler.Compiler.compile(Compiler.java:340)
	at org.apache.jasper.JspCompilationContext.compile(JspCompilationContext.java:657)
	at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:357)
	at org.apache.jasper.servlet.JspServlet._serviceJspFile(JspServlet.java:390)
	at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java)
	at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:334)

It works when the ternary operation doesn't appear inside an attribute or instead of eq '==' is used.
Comment 1 Michael Simons 2014-01-18 13:09:13 UTC
Happens with all named operators

<span class="${foo == 'bar' and 1==1 ? 'bar-span' : 'other-span'}">span</span>	


will break as well.

If used in jspx it only works now as 

<span class="${foo == 'bar' &amp;&amp; 1==1 ? 'bar-span' : 'other-span'}">span</span>	

which is not a really acceptable solution.
Comment 2 Konstantin Kolinko 2014-01-18 15:34:27 UTC
Ack. Reproducible with your sample file from Comment 0 minus the 'xmlns:c' and 'xmlns:fmt' attributes.


> javax.el.ELException: Failed to parse the expression [${currentControllereq'News'?'selectedItem':''}]

Notes:

1. "currentControllereq" is printed without spaces in the above message. Also the rest of whitespaces are also missing in the message.

2. A workaround is to add braces around the variable name.
The following does work:

... class="${(currentController) eq 'News' ? 'selectedItem' : ''}" ...


3. This issue happens when EL in the value of a tag attribute.

This issue does not happen when EL is in the plain text (body of a tag).
The following does work successfully:

<jsp:root version="2.0" xmlns:jsp="http://java.sun.com/JSP/Page"
><jsp:directive.page contentType="text/plain"/>
${currentController eq 'News' ? 'selectedItem' : ''}</jsp:root>
Comment 3 Konstantin Kolinko 2014-01-18 15:44:16 UTC
(In reply to Konstantin Kolinko from comment #2)
> Ack. Reproducible with your sample file from Comment 0 minus the 'xmlns:c'
> and 'xmlns:fmt' attributes.
> 

Reproducible in 8.0-RC10 and 6.0.38 (release candidate) as well.
Comment 4 Mark Thomas 2014-01-18 17:23:11 UTC
I suspected when I read this report that it was a regression caused by the fix for Bug 55198 and given the versions of Tomcat affected it looks to be very much the case (although I haven't confirmed it yet).
Comment 5 Mark Thomas 2014-01-18 19:46:43 UTC
Fixed in 8.0.x for 8.0.0 onwards and in 7.0.x for 7.0.51 onwards.

Proposed for 6.0.x.
Comment 6 Mark Thomas 2014-01-18 21:04:27 UTC
*** Bug 56031 has been marked as a duplicate of this bug. ***
Comment 7 Michael Simons 2014-01-18 21:11:32 UTC
Konstantin: Your addendum is fully correct, i can confirm this.

Thanks for your kind reply and the quick solution, highly appreciated. Also thanks for the workaround, but i guess i'll wait to 7.0.51.
Comment 8 Konstantin Kolinko 2014-01-20 12:10:03 UTC
Created attachment 31231 [details]
test.jspx - Failure to trim leading space from fn prefix

Re: r1559555

Not yet.

Testing current 7.0.x (at r1559663) I see the following error
1. Place attached test.jspx into webapps/examples
2. Access http://localhost:8080/examples/test.jspx
3. Expected: "[1]". Actual:

org.apache.jasper.JasperException: /test.jspx (line: 7, column: 63) The attribute prefix  fn does not correspond to any imported tag library
 org.apache.jasper.compiler.DefaultErrorHandler.jspError(DefaultErrorHandler.java:42)
 org.apache.jasper.compiler.ErrorDispatcher.dispatch(ErrorDispatcher.java:443)
 org.apache.jasper.compiler.ErrorDispatcher.jspError(ErrorDispatcher.java:149)
 org.apache.jasper.compiler.Validator$ValidateVisitor$1FVVisitor.visit(Validator.java:1563)
 ...
 org.apache.jasper.compiler.Node$Nodes.visit(Node.java:2375)
 org.apache.jasper.compiler.Validator.validateExDirectives(Validator.java:1817)
 org.apache.jasper.compiler.Compiler.generateJava(Compiler.java:217)
 ...

In the message the prefix is printed as " fn" with leading whitespace.
The expression on the page is ${1 + fn:length(list)}
Comment 9 Mark Thomas 2014-01-20 14:32:26 UTC
If fixed the white space issue in 8.0.x and 7.0.x and added it to the proposal for 6.0.x
Comment 10 Mark Thomas 2014-01-23 21:53:02 UTC
This has been fixed in 6.0.x and will be included in 6.0.39 onwards.
Comment 11 Mark Thomas 2014-02-10 09:09:24 UTC
*** Bug 56124 has been marked as a duplicate of this bug. ***
Comment 12 Santosh 2014-02-10 09:20:44 UTC
It seems this issue is fixed now. Can anyone advice in which 7.0.x version can I expect the fix to be delivered?
Comment 13 Yuvaraju 2014-02-10 10:18:44 UTC
(In reply to Santosh from comment #12)
> It seems this issue is fixed now. Can anyone advice in which 7.0.x version
> can I expect the fix to be delivered?

Santosh,

Mark Thomson mentioned that will be in 8.0.0 and 7.0.51 onwards. Not sure when will the 7.0.51 be available.. :-)