2

Before I write a custom tag to do this, I want to be sure I didn't miss something in EL or JSTL.

There's an int returned by a bean property that is stored in the request scope. I need to output it as hex. But after searching, it seems there is no way in JSP to output an int that is returned from a bean by EL thusly...

${someBean.someInt}

...as a hexadecimal value, in the way that...

<%= String.format("0x%X", someBean.getSomeInt()) %>

...would.

Am I correct? If not, how is it done? (Our departmental coding standards disallow Java directly included in JSP using the <% %> syntax. We have to write tags if we can't find something already available.)

John Fitzpatrick
  • 4,207
  • 7
  • 48
  • 71

1 Answers1

2

You can't do this in EL or using JSTL's formatting tags. Writing a custom tag or EL function is the best option.

If it's just one int you have to format you could add a String getter to your bean to do the formatting

public String getSomeIntAsHex(){
  return String.format("0x%X", someInt);
}

and then you could use that getter in EL

${someBean.someIntAsHex}
Community
  • 1
  • 1
Jasper de Vries
  • 19,370
  • 6
  • 64
  • 102
  • I used your second suggestion. But I'm curious about the EL Function approach. How do I output text to the page from within an EL Function? Or is the return value of the function automatically output to the page? – John Fitzpatrick Sep 12 '12 at 12:07
  • Something like `${myTagLib:intToHex(someBean.someInt)}` which you could directly output or use in ``. – Jasper de Vries Sep 12 '12 at 12:11