You are viewing a plain text version of this content. The canonical link for it is here.
Posted to taglibs-user@tomcat.apache.org by Howard Lin <Ho...@vonage.com> on 2003/06/02 15:33:38 UTC

forTokens problem

I'm trying to use c:forTokens to parse a string, using "\n" as the delimiter and it seems it doesn't work. The jsp code is like this:
<c:forTokens items="my_string" delims="\n" var="foo">
	<td><c:out value="${foo}"/></td>
</c:forTokens>
"${foo}" shows the whole entire string instead of the token. but if I use StringTokenizer to parse the string in a script, I got the individual token. So I suspect c:forTokens might be the problem. Thanks,

Howard Lin

---------------------------------------------------------------------
To unsubscribe, e-mail: taglibs-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: taglibs-user-help@jakarta.apache.org


Re: forTokens problem

Posted by Pierre Delisle <Pi...@Sun.COM>.
Howard Lin wrote:
> I'm trying to use c:forTokens to parse a string, using "\n" as the delimiter and it seems it doesn't work. The jsp code is like this:
> <c:forTokens items="my_string" delims="\n" var="foo">
> 	<td><c:out value="${foo}"/></td>
> </c:forTokens>
> "${foo}" shows the whole entire string instead of the token. but if I use StringTokenizer to parse the string in a script, I got the individual token. So I suspect c:forTokens might be the problem. Thanks,

Howard Lin wrote:
> I'm trying to use c:forTokens to parse a string, using "\n" as the delimiter and it seems it doesn't work. The jsp code is like this:
> <c:forTokens items="my_string" delims="\n" var="foo">
> 	<td><c:out value="${foo}"/></td>
> </c:forTokens>
> "${foo}" shows the whole entire string instead of the token. but if I use StringTokenizer to parse the string in a script, I got the individual token. So I suspect c:forTokens might be the problem. Thanks,

Howard,

It is not a problem with c:forTokens, but rather the fact that 
there is no way to pass special Java characters as String literals
in an attribute value.

The "\n" is being interpreted as the two characters '\' and
'n'.

One way to get the behavior you want is to make sure the
"\n" is really passed as a Java String.

For example:

   JSTL 1.1 (assuming myString is a scoped variable)

   <c:forTokens items="${myString}" delims='<%="\n"%>' var="token">
     <c:out value="${token}"/><br>
   </c:forTokens>

   JSTL 1.0 (assuming myString is a scripting variable)

   <c_rt:forTokens items="<%=myString%>" delims='<%="\n"%>' var="token">
     <c:out value="${token}"/><br>
   </c_rt:forTokens>

   or
   <% 
      String lf = "\n"; 
      pageContext.setAttribute("lf", lf);
   %>
   <c:forTokens items="${myString}" delims="${lf}" var="token">
     <c:out value="${token}"/><br>
   </c:forTokens>

    -- Pierre 

[and yes, maybe the EL should recognize these special character
sequences... passed the issue to the JSP spec leads]


---------------------------------------------------------------------
To unsubscribe, e-mail: taglibs-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: taglibs-user-help@jakarta.apache.org


Re: forTokens problem

Posted by Pierre Delisle <Pi...@Sun.COM>.
Howard Lin wrote:
> I'm trying to use c:forTokens to parse a string, using "\n" as the delimiter and it seems it doesn't work. The jsp code is like this:
> <c:forTokens items="my_string" delims="\n" var="foo">
> 	<td><c:out value="${foo}"/></td>
> </c:forTokens>
> "${foo}" shows the whole entire string instead of the token. but if I use StringTokenizer to parse the string in a script, I got the individual token. So I suspect c:forTokens might be the problem. Thanks,

Howard Lin wrote:
> I'm trying to use c:forTokens to parse a string, using "\n" as the delimiter and it seems it doesn't work. The jsp code is like this:
> <c:forTokens items="my_string" delims="\n" var="foo">
> 	<td><c:out value="${foo}"/></td>
> </c:forTokens>
> "${foo}" shows the whole entire string instead of the token. but if I use StringTokenizer to parse the string in a script, I got the individual token. So I suspect c:forTokens might be the problem. Thanks,

Howard,

It is not a problem with c:forTokens, but rather the fact that 
there is no way to pass special Java characters as String literals
in an attribute value.

The "\n" is being interpreted as the two characters '\' and
'n'.

One way to get the behavior you want is to make sure the
"\n" is really passed as a Java String.

For example:

   JSTL 1.1 (assuming myString is a scoped variable)

   <c:forTokens items="${myString}" delims='<%="\n"%>' var="token">
     <c:out value="${token}"/><br>
   </c:forTokens>

   JSTL 1.0 (assuming myString is a scripting variable)

   <c_rt:forTokens items="<%=myString%>" delims='<%="\n"%>' var="token">
     <c:out value="${token}"/><br>
   </c_rt:forTokens>

   or
   <% 
      String lf = "\n"; 
      pageContext.setAttribute("lf", lf);
   %>
   <c:forTokens items="${myString}" delims="${lf}" var="token">
     <c:out value="${token}"/><br>
   </c:forTokens>

    -- Pierre 

[and yes, maybe the EL should recognize these special character
sequences... passed the issue to the JSP spec leads]


---------------------------------------------------------------------
To unsubscribe, e-mail: taglibs-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: taglibs-user-help@jakarta.apache.org