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 Francois Beausoleil <fb...@users.sourceforge.net> on 2003/12/29 03:19:31 UTC

How to cast String to Number in JSTL EL ?

Hi !

I have a slight problem.  I am building a paged results browser, and I
have a problem with my next action.

The root of the problem is because JSTL thinks that some values are
strings, and not numbers.

The code I am running is:
<fmt:formatNumber var="lastPageNo" value="${numberOfContacts / count}"
        minFractionDigits="0" maxFractionDigits="0"/>
<c:if test="${numberOfContacts le count}">
    <c:set var="lastPageNo" value="1"/>
</c:if>
<c:if test="${(numberOfContacts gt count) and ((numberOfContacts mod
count) ne 0)}">
    <c:set var="lastPageNo" value="${lastPageNo + 1}"/>
</c:if>

    pageNo:                <c:out value="${pageNo}"/>
    pageNo.class:          <c:out value="${pageNo.class}"/>
    lastPageNo:            <c:out value="${lastPageNo}"/>
    lastPageNo.class:      <c:out value="${lastPageNo.class}"/>
    pageNo lt lastPageNo:  <c:out value="${pageNo lt lastPageNo}"/>

This code does arithmetics on the numbers in some cases, and when these
run, everything is fine.  *But*, when the arithmetic operations do not
run, I get the following in my comment block:
    pageNo:                2
    pageNo.class:          class java.lang.String
    lastPageNo:            13
    lastPageNo.class:      class java.lang.String
    pageNo lt lastPageNo:  false

As you can see, JSTL considers both pageNo and lastPageNo to be strings.
I finally resolved my problem by switching to a choose, and adding an
otherwise block that adds 0 with lastPageNo, converting to a number.

All of this boils down to:  is there the equivalent of a Java cast in
JSTL ?  If there were, I could simply cast my pageNo and lastPageNo when
doing my tests, and everything would be fine.

Thanks for any help !
François
Developer of Java Gui Builder
http://jgb.sourceforge.net/

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


Re: How to cast String to Number in JSTL EL ?

Posted by Francois Beausoleil <fb...@users.sourceforge.net>.
Thanks for the tip :)

You are right.  Maybe I should move these out of the JSP.

Bye !
François

On Sun, 28 Dec 2003 22:42:23 -0500, "Kris Schneider" <kr...@dotech.com>
said:
> It's not a direct answer to your question, but it seems like the work 
> you're doing is more suited to a servlet or component of an MVC/Model 2 
> framework. In other words, calculate the actual values you need before 
> forwarding to the JSP.
> 
> AFAIK, JSTL doesn't directly support what you're looking for. When 
> necessary, it performs automatic data conversions to make it easier to 
> match application data with the expected types of things like tag/action 
> attributes. Again, if possible, I'd pull your calculations out of the 
> JSP altogether...
> 
> Francois Beausoleil wrote:
> 
> > Hi !
> > 
> > I have a slight problem.  I am building a paged results browser, and I
> > have a problem with my next action.
> > 
> > The root of the problem is because JSTL thinks that some values are
> > strings, and not numbers.
> > 
> > The code I am running is:
> > <fmt:formatNumber var="lastPageNo" value="${numberOfContacts / count}"
> >         minFractionDigits="0" maxFractionDigits="0"/>
> > <c:if test="${numberOfContacts le count}">
> >     <c:set var="lastPageNo" value="1"/>
> > </c:if>
> > <c:if test="${(numberOfContacts gt count) and ((numberOfContacts mod
> > count) ne 0)}">
> >     <c:set var="lastPageNo" value="${lastPageNo + 1}"/>
> > </c:if>
> > 
> >     pageNo:                <c:out value="${pageNo}"/>
> >     pageNo.class:          <c:out value="${pageNo.class}"/>
> >     lastPageNo:            <c:out value="${lastPageNo}"/>
> >     lastPageNo.class:      <c:out value="${lastPageNo.class}"/>
> >     pageNo lt lastPageNo:  <c:out value="${pageNo lt lastPageNo}"/>
> > 
> > This code does arithmetics on the numbers in some cases, and when these
> > run, everything is fine.  *But*, when the arithmetic operations do not
> > run, I get the following in my comment block:
> >     pageNo:                2
> >     pageNo.class:          class java.lang.String
> >     lastPageNo:            13
> >     lastPageNo.class:      class java.lang.String
> >     pageNo lt lastPageNo:  false
> > 
> > As you can see, JSTL considers both pageNo and lastPageNo to be strings.
> > I finally resolved my problem by switching to a choose, and adding an
> > otherwise block that adds 0 with lastPageNo, converting to a number.
> > 
> > All of this boils down to:  is there the equivalent of a Java cast in
> > JSTL ?  If there were, I could simply cast my pageNo and lastPageNo when
> > doing my tests, and everything would be fine.
> > 
> > Thanks for any help !
> > François
> > Developer of Java Gui Builder
> > http://jgb.sourceforge.net/
> 
> -- 
> Kris Schneider <ma...@dotech.com>
> D.O.Tech       <http://www.dotech.com/>
> 
> 
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: taglibs-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: taglibs-user-help@jakarta.apache.org
> 
Developer of Java Gui Builder
http://jgb.sourceforge.net/

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


Re: How to cast String to Number in JSTL EL ?

Posted by Kris Schneider <kr...@dotech.com>.
It's not a direct answer to your question, but it seems like the work 
you're doing is more suited to a servlet or component of an MVC/Model 2 
framework. In other words, calculate the actual values you need before 
forwarding to the JSP.

AFAIK, JSTL doesn't directly support what you're looking for. When 
necessary, it performs automatic data conversions to make it easier to 
match application data with the expected types of things like tag/action 
attributes. Again, if possible, I'd pull your calculations out of the 
JSP altogether...

Francois Beausoleil wrote:

> Hi !
> 
> I have a slight problem.  I am building a paged results browser, and I
> have a problem with my next action.
> 
> The root of the problem is because JSTL thinks that some values are
> strings, and not numbers.
> 
> The code I am running is:
> <fmt:formatNumber var="lastPageNo" value="${numberOfContacts / count}"
>         minFractionDigits="0" maxFractionDigits="0"/>
> <c:if test="${numberOfContacts le count}">
>     <c:set var="lastPageNo" value="1"/>
> </c:if>
> <c:if test="${(numberOfContacts gt count) and ((numberOfContacts mod
> count) ne 0)}">
>     <c:set var="lastPageNo" value="${lastPageNo + 1}"/>
> </c:if>
> 
>     pageNo:                <c:out value="${pageNo}"/>
>     pageNo.class:          <c:out value="${pageNo.class}"/>
>     lastPageNo:            <c:out value="${lastPageNo}"/>
>     lastPageNo.class:      <c:out value="${lastPageNo.class}"/>
>     pageNo lt lastPageNo:  <c:out value="${pageNo lt lastPageNo}"/>
> 
> This code does arithmetics on the numbers in some cases, and when these
> run, everything is fine.  *But*, when the arithmetic operations do not
> run, I get the following in my comment block:
>     pageNo:                2
>     pageNo.class:          class java.lang.String
>     lastPageNo:            13
>     lastPageNo.class:      class java.lang.String
>     pageNo lt lastPageNo:  false
> 
> As you can see, JSTL considers both pageNo and lastPageNo to be strings.
> I finally resolved my problem by switching to a choose, and adding an
> otherwise block that adds 0 with lastPageNo, converting to a number.
> 
> All of this boils down to:  is there the equivalent of a Java cast in
> JSTL ?  If there were, I could simply cast my pageNo and lastPageNo when
> doing my tests, and everything would be fine.
> 
> Thanks for any help !
> François
> Developer of Java Gui Builder
> http://jgb.sourceforge.net/

-- 
Kris Schneider <ma...@dotech.com>
D.O.Tech       <http://www.dotech.com/>



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


Re: How to cast String to Number in JSTL EL ?

Posted by "...." <ce...@basebeans.com>.
For paging, take a look at the displaytag on sf.net.

.V

Jason Lea wrote:
> I had a similar problem (paging and JSTL) and managed to get it 
> working.  The <c:set var="lastPageNo" value="1"/> code is setting the 
> lastPageNo to a string and then later on it is doing a string 
> concatenation on it so you get "13" instead of "4"
> 
> JSTL will coerce it into an integer if you assign it this way:
> 
> <c:set var="lastPageNo" value="${1}"/>
> 
> According to the JSTL spec any value="sometext" will always be a 
> string.  Using the ${} makes JSTL evaluate it and decide you have an 
> integer there.
> 
> 
> Francois Beausoleil wrote:
> 
>> Hi !
>>
>> I have a slight problem.  I am building a paged results browser, and I
>> have a problem with my next action.
>>
>> The root of the problem is because JSTL thinks that some values are
>> strings, and not numbers.
>>
>> The code I am running is:
>> <fmt:formatNumber var="lastPageNo" value="${numberOfContacts / count}"
>>        minFractionDigits="0" maxFractionDigits="0"/>
>> <c:if test="${numberOfContacts le count}">
>>    <c:set var="lastPageNo" value="1"/>
>> </c:if>
>> <c:if test="${(numberOfContacts gt count) and ((numberOfContacts mod
>> count) ne 0)}">
>>    <c:set var="lastPageNo" value="${lastPageNo + 1}"/>
>> </c:if>
>>
>>    pageNo:                <c:out value="${pageNo}"/>
>>    pageNo.class:          <c:out value="${pageNo.class}"/>
>>    lastPageNo:            <c:out value="${lastPageNo}"/>
>>    lastPageNo.class:      <c:out value="${lastPageNo.class}"/>
>>    pageNo lt lastPageNo:  <c:out value="${pageNo lt lastPageNo}"/>
>>
>> This code does arithmetics on the numbers in some cases, and when these
>> run, everything is fine.  *But*, when the arithmetic operations do not
>> run, I get the following in my comment block:
>>    pageNo:                2
>>    pageNo.class:          class java.lang.String
>>    lastPageNo:            13
>>    lastPageNo.class:      class java.lang.String
>>    pageNo lt lastPageNo:  false
>>
>> As you can see, JSTL considers both pageNo and lastPageNo to be strings.
>> I finally resolved my problem by switching to a choose, and adding an
>> otherwise block that adds 0 with lastPageNo, converting to a number.
>>
>> All of this boils down to:  is there the equivalent of a Java cast in
>> JSTL ?  If there were, I could simply cast my pageNo and lastPageNo when
>> doing my tests, and everything would be fine.
>>
>> Thanks for any help !
>> François
>> Developer of Java Gui Builder
>> http://jgb.sourceforge.net/
>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: taglibs-user-unsubscribe@jakarta.apache.org
>> For additional commands, e-mail: taglibs-user-help@jakarta.apache.org
>>
>>
>>  
>>
> 


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


Re: How to cast String to Number in JSTL EL ?

Posted by Jason Lea <ja...@kumachan.net.nz>.
I had a similar problem (paging and JSTL) and managed to get it 
working.  The <c:set var="lastPageNo" value="1"/> code is setting the 
lastPageNo to a string and then later on it is doing a string 
concatenation on it so you get "13" instead of "4"

JSTL will coerce it into an integer if you assign it this way:

<c:set var="lastPageNo" value="${1}"/>

According to the JSTL spec any value="sometext" will always be a 
string.  Using the ${} makes JSTL evaluate it and decide you have an 
integer there.


Francois Beausoleil wrote:

>Hi !
>
>I have a slight problem.  I am building a paged results browser, and I
>have a problem with my next action.
>
>The root of the problem is because JSTL thinks that some values are
>strings, and not numbers.
>
>The code I am running is:
><fmt:formatNumber var="lastPageNo" value="${numberOfContacts / count}"
>        minFractionDigits="0" maxFractionDigits="0"/>
><c:if test="${numberOfContacts le count}">
>    <c:set var="lastPageNo" value="1"/>
></c:if>
><c:if test="${(numberOfContacts gt count) and ((numberOfContacts mod
>count) ne 0)}">
>    <c:set var="lastPageNo" value="${lastPageNo + 1}"/>
></c:if>
>
>    pageNo:                <c:out value="${pageNo}"/>
>    pageNo.class:          <c:out value="${pageNo.class}"/>
>    lastPageNo:            <c:out value="${lastPageNo}"/>
>    lastPageNo.class:      <c:out value="${lastPageNo.class}"/>
>    pageNo lt lastPageNo:  <c:out value="${pageNo lt lastPageNo}"/>
>
>This code does arithmetics on the numbers in some cases, and when these
>run, everything is fine.  *But*, when the arithmetic operations do not
>run, I get the following in my comment block:
>    pageNo:                2
>    pageNo.class:          class java.lang.String
>    lastPageNo:            13
>    lastPageNo.class:      class java.lang.String
>    pageNo lt lastPageNo:  false
>
>As you can see, JSTL considers both pageNo and lastPageNo to be strings.
>I finally resolved my problem by switching to a choose, and adding an
>otherwise block that adds 0 with lastPageNo, converting to a number.
>
>All of this boils down to:  is there the equivalent of a Java cast in
>JSTL ?  If there were, I could simply cast my pageNo and lastPageNo when
>doing my tests, and everything would be fine.
>
>Thanks for any help !
>François
>Developer of Java Gui Builder
>http://jgb.sourceforge.net/
>
>---------------------------------------------------------------------
>To unsubscribe, e-mail: taglibs-user-unsubscribe@jakarta.apache.org
>For additional commands, e-mail: taglibs-user-help@jakarta.apache.org
>
>
>  
>

-- 
Jason Lea



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