aspose file tools*
The moose likes JSP and the fly likes JSTL Formatting Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » JSP
Bookmark "JSTL Formatting" Watch "JSTL Formatting" New topic
Author

JSTL Formatting

Eric Martin
Greenhorn

Joined: Jul 15, 2005
Posts: 22
Hello, any one know of a open source tag library that allows formatting of values. For example, I have 444444444 and I want it formatted as a social security number 444-44-4444. Same with phone numbers, etc. I found the fmt:formatNumber, but it does not work for socials and phone.

TIA.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61426
    
  67

You could write your own with a few lines of code.

If you've never written a custom tag, something this basic would be a great way to learn.
[ August 22, 2005: Message edited by: Bear Bibeault ]

[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Eric Martin
Greenhorn

Joined: Jul 15, 2005
Posts: 22
We are using JSP 1.2 and I'm afraid that I haven't done any custom tag writing yet, so I was hoping that there would be plenty of existing ones out there

If nobody knows of one...I may have to roll up my sleeves and figure it out.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61426
    
  67

You know where to come to ask questions. Your starting point would be the javax.servlet.jsp.tagext.TagSupport class.

The hardest part (if you can call it that) is setting up a TLD for the first time.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61426
    
  67

P.S. If you are using Tomcat 4, check out the examples.
Eric Martin
Greenhorn

Joined: Jul 15, 2005
Posts: 22
It's mostly working now...just one little issue =)

I have the following in my jsp page:



But myBean.SSN is not being rendered and so I get as the value from my tag.

How is this supposed to work? I tried looking at the source for some other taglibs and could not figure it out.

EDIT: Actually, what I really want is the ability to use EL directly in my tag:

How do I do that?

Thanks.

[ August 23, 2005: Message edited by: Eric Martin ]
[ August 23, 2005: Message edited by: Eric Martin ]
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61426
    
  67

You can't use a tag as the attribute of another tag.

Under JSP 2.0, you would simply use:



but since you are still mired in the JSP 1.2 world, you'll need to use a scriptlet expression to fetch the attribute value. Perhaps something along the lines of:



P.S. If you click 'disable smilies' when posting, your code will look less surprised.
Eric Martin
Greenhorn

Joined: Jul 15, 2005
Posts: 22
Thanks for the posting tips =) I edited my post while you were posting...I realize now that I can't nest the tags, but what about EL?

When using JSTL in 1.2, I can use ${myBean.ssn}, is this complicated to implement in my own tag?

Thanks.
[ August 23, 2005: Message edited by: Eric Martin ]
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61426
    
  67

Under JSP 1.2, the EL is only available to JSTL tags.

That is one of the biggest differences between JSP 1.2 and 2.0 -- the EL is interpreted by the container in JSP 2.0, but only by JSTL tags in JSP 1.2.
Eric Martin
Greenhorn

Joined: Jul 15, 2005
Posts: 22
Bummer...so is there any way to utilize JSTL from my tag to enable EL?
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61426
    
  67

Not reliably.

If you'd rather avoid scriptlet expressions on the page until you have the opportunity to switch to JSP 2.0 (what's holding that up, by the way?), one technique I've seen used is to use the JSTL to set up a scoped variable and to pass the name of the scoped variable, as opposed to its value, to the custom tag, and then the custom tag fetches the value of the scoped variable using the pageContext.getAttribute() method.

So perhaps something like:



Then, within the tag:



This looks a lot cleaner and creates no artificial binding between the page and the tag (since you pass the name of the variable along rather than hard-coding a name that both sides agree upon -- bad practice).

The only issue I have with this approach is that when JSP 2.0 time comes, refactoring the tag to take the value rather than the name is a bit more work. Of course, this will still work as is under JSP 2.0 -- it just seems a bit of an odd way to do this in that environment.
Eric Martin
Greenhorn

Joined: Jul 15, 2005
Posts: 22
Thanks...I'll try that out. I'd love to switch to JSP 2.0, but we are bound to Sun Java Systems Web Server 6.1, JSP 1.2 and Servlet 2.3
Stefan Evans
Bartender

Joined: Jul 06, 2005
Posts: 1018
Another approach would be to use the body of the tag to put the number in:


Evaluate the body "buffered", grab the body, format it, and then print out the formatted value.
[ August 23, 2005: Message edited by: Stefan Evans ]
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61426
    
  67

Good alternative approach Stefan. I'd just worry that dealing with body tags might be a bit daunting for a first-time tag writer.

It is a nice and clean approach to the problem and would require little/no refactoring for JSP 2.0.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: JSTL Formatting