• Post Reply Bookmark Topic Watch Topic
  • New Topic

How to convert String into integer in JSTL  RSS feed

 
Tarun Oohri
Ranch Hand
Posts: 189
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Everybody,
I am having problem in converting JSTL variable into integer type in JSP (not using Spring). I am looking to do something like below:

Thanks In Advance!!
Cheers
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65833
134
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why is totalAmount a String in the first place? That's what needs to be fixed. Or are you making it a String via the set action? Why are you using the set action at all?
 
Tarun Oohri
Ranch Hand
Posts: 189
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Thanks Bear Sir for replying .. 'totalAmount' is a String because it is being iterate from a list containing String objects , 'totalAmount' is one of the values from that list. Secondly i am using 'set' action so i could use it as a variable eg: <c:when test=""> tag. I would like to add that both the above code statements were not written in such order...2nd statement is written somewhere below , thats why i put 'totalAmount' value in a variable.

Any solution to this please?
 
Paul Clapham
Sheriff
Posts: 22185
38
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Okay, then why don't you convert it to an integer when you add it to the list?
 
Tarun Oohri
Ranch Hand
Posts: 189
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul Clapham wrote:Okay, then why don't you convert it to an integer when you add it to the list?

Thanks Paul , this could be possible solution. Actually that is is like List<String>, so have to remove this type safety and allow list to contain different type of objects .... Is that what you are trying to say ?
 
Paul Clapham
Sheriff
Posts: 22185
38
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, that's the next thing I expected to have to say. Why do you have a list of different types of objects? In fact why do you have a list at all?
 
Tarun Oohri
Ranch Hand
Posts: 189
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul Clapham wrote:Yes, that's the next thing I expected to have to say. Why do you have a list of different types of objects? In fact why do you have a list at all?

Actually this list is being used to show set of data from the database, showing it in tabular way. That's why using list.
 
shail singh
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
can you change the type of list as integer if at all you know that the value stored is to be used as integer, alternatively you can write a scriptlet inside and manipulate the value although its not a good practice.
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65833
134
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
shail singh wrote:alternatively you can write a scriptlet inside and manipulate the value although its not a good practice.

In fact, it is such bad practice that it should never be recommended at all.
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65833
134
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What it comes down to is that you are sending bad data to the JSP. The place to fix the data is before it gets to the JSP. Doing weird machinations in a JSP in order to deal with a poor data model is definitely not the way to go.
 
Stefan Evans
Bartender
Posts: 1834
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
erm - Doesn't EL already "do the right thing" ?
It will automagically do the conversion from string to long if the other argument is a long.

So your code should be:



If total_amt is not actually a number this will throw a parse error at runtime.
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65833
134
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Probably, (didn't test it) but I still think that scrubbed data should always be sent to a JSP.
 
Dave Tolls
Ranch Hand
Posts: 2504
27
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bear Bibeault wrote:Probably, (didn't test it) but I still think that scrubbed data should always be sent to a JSP.


This.
The data that goes to the JSP should be in a form that the JSP can use with a minimum of effort.
If you find yourself doing shenanigans on the data in the JSP then change the view model.
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65833
134
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dave Tolls wrote:
Bear Bibeault wrote:Probably, (didn't test it) but I still think that scrubbed data should always be sent to a JSP.

This.
The data that goes to the JSP should be in a form that the JSP can use with a minimum of effort.
If you find yourself doing shenanigans on the data in the JSP then change the view model.

I regret that I can only give this post one thumbs-up.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!