Currently I have something like this in my JSP
<c:url value="/teams/${contact.id}/${contact.name}" />
The important part of my URL is the ID, I just put the name on it for SEO purposes (just like stackoverflow.com does).
I was just wondering if there is a quick and clean way to encode the name (change spaces per +, latin chars removal, etc). I'd like it to be like this:
<c:url value="/teams/${contact.id}/${supercool(contact.name)}" />
Is there a function like that out there or should I make my own?
Nothing like that is available in JSTL functions. You'll need to create your own. I'd by the way rather replace spaces by
-
.To the point, you want to perform the following steps:
Lowercase the string.
Normalize all characters and get rid of all diacritical marks.
Replace all remaining non-alphanumeric characters by
-
and collapse when necessary.You can wrap this in an EL function:
Which you register in a
/WEB-INF/functions.tld
like follows:Which you can use in JSP as follows:
Look for server.urlencode, all the major server side languages today have them.