GeeCON Prague 2014*
The moose likes JSP and the fly likes string to Integer comparison in jstl Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » JSP
Bookmark "string to Integer comparison in jstl" Watch "string to Integer comparison in jstl" New topic
Author

string to Integer comparison in jstl

Rick Prows
Greenhorn

Joined: Feb 16, 2012
Posts: 10
This seems like a dumb question to me - but I can't figure out what I am doing incorrectly.

In my jsp page i am trying to do the following:


financialInstitutionNumber is an Integer and in this example is an 11
securityBean.divisionNumber is a String and in this case is 011 (fixed 3 characters in length)

It is not getting inside this If statement. Is there a way I can cast the String to an Integer in this code?

Not sure why I am drawing a blank - but I couldn't find anything online that shows it.
Jesus Angeles
Ranch Hand

Joined: Feb 26, 2005
Posts: 2057
If all else fails, convert the Integer into String.

Add a getAxxxx method in finInst class which returns the 0-left-padded-String-version of that Integer; and thereby use "finInst.axxxx ==" in jstl.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61314
    
  66

Why is the data so dirty to begin with? Clean it up before sending it along to the JSP. The JSP is no place to be performing gyrations on data that's not in a good format for consumption.

P.S. If I recall coercion rules correctly, the values are being coerced to strings, so "11" != "011".


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Rick Prows
Greenhorn

Joined: Feb 16, 2012
Posts: 10
They are coming from different sources. The security bean is a corporate bean and division number is stored as a character.

the rest of the code is at the application layer and the division number is an integer in that application.

i have a solution, definately not the best solution, but it does seem to work. I'm sure there is probably an instance where it might show when it shouldn't, but the solution I used is:

fn:contains(securityBean.divisionNumber,finInst.financialInstitutionNumber)

where as it checks to see if the interger value is contained in the String value.
Not the best solution, because division 11 would work in 011, 511, 611, and 110.

still looking for a better long-term approach though
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61314
    
  66

If you can't "fix" the data in the page controller, at minimum I'd write an EL function that converted "011" to 11 for comparison. I would not consider your current comprise an acceptable solution.
Rick Prows
Greenhorn

Joined: Feb 16, 2012
Posts: 10
ok - I went with Jesus recommendation. I put a getter in my base action that converted my securityBean string to an Integer and then I referenced that in my jsp.

Thanks all for your time and recommendations.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: string to Integer comparison in jstl