• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

JSTL if tag question

 
Timothy Stone
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There appears to be a subtlety to the `test' attribute that could use some clarification.

What is known:

single quotes or double quotes may be used in most tags and the EL.

However, the following fails with `equal symbol expected':

<c:if test="${userType eq "member"}">

The following will compile and behave as expected:

<c:if test="${userType eq 'member'}">

Where is this noted in the spec? Or is it noted in the JSTL spec?

Sidebar... HF:S&JSP could be less ambiguous about this subtlety on pg 441 me thinks. The scribble implies that both forms should work, by its presence, not by example.

Can anyone elaborate?

Thanks!
Tim
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65342
97
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This has nothing to do with JSTL or EL specifically, but with the way JSP parses custom tags (JSTL or otherwise). If you use the double quotes as delimiters, using them within the value will boof up the parsing. Same reason that you need to escape quote characters in Java string literals.
[ March 01, 2008: Message edited by: Bear Bibeault ]
 
Timothy Stone
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I see the logic... but cite this example from actual production code in a JSP:



Note the nested double quotes the JSP. Some template code uses the response/return of the static method. This hasn't "boof'd" on parsing. Is it a scriptlet v. EL/JSTL question then?

Thanks!
Tim
 
Marc Peabody
pie sneak
Sheriff
Posts: 4727
Mac Ruby VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Timothy Stone:
I see the logic... but cite this example from actual production code in a JSP:



Note the nested double quotes the JSP. Some template code uses the response/return of the static method. This hasn't "boof'd" on parsing. Is it a scriptlet v. EL/JSTL question then?

The <a> tag isn't a rendered tag. It's just template text.

You really have to think through WHY it's a problem in an evaluated taglib tag. Don't just say "Oh, it goofs up because it's a tag" and move on. Think why.

In an evaluated tag, the tag renderer has to collect all the attributes. If it sees that your attribute value begins with a double quote, it's going to assume that the next double quote marks the end of the attribute value. Same with if your opening character were a single quote, it expects the ending character to be a single quote.

Now, for a tag like the <a> tag it's just template text that doesn't get evaluated on the server side. Nothing on the server side is trying to harvest its attribute values.

The difference can seem confusing because to the untrained eye both are simply the same format - an attribute in a tag. However, there's a huge difference in this case between a template text tag that the server side ignores and a taglib tag that gets evaluated and rendered on the server side.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic