views:

294

answers:

2

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?

+2  A: 

Nothing like that is available in JSTL functions. You'll need to create your own. I'd by the way rather replace spaces by -.

Update: to the point, you want to complete the following steps:

  1. Lowercase the string.

    string = string.toLowerCase();
    
  2. Normalize all characters and get rid of all diacritical marks.

    string = Normalizer.normalize(string, Form.NFD).replaceAll("\\p{InCombiningDiacriticalMarks}+", "");
    
  3. Replace all remaining non-alphanumeric characters by - and collapse when necessary.

    string = string.replaceAll("[^\\p{Alnum}]+", "-");
    

You can wrap this in an EL function the way as described at the bottom of my answer here.

package com.example;

import java.text.Normalizer;
import java.text.Normalizer.Form;

public final class Functions {
     private Functions() {}

     public static String prettyURL(String string) {
         return Normalizer.normalize(string.toLowerCase(), Form.NFD)
             .replaceAll("\\p{InCombiningDiacriticalMarks}+", "")
             .replaceAll("[^\\p{Alnum}]+", "-");
     }
}

And a /WEB-INF/functions.tld like follows:

<?xml version="1.0" encoding="UTF-8" ?>
<taglib 
    xmlns="http://java.sun.com/xml/ns/javaee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-jsptaglibrary_2_1.xsd"
    version="2.1">

    <display-name>Custom Functions</display-name>    
    <tlib-version>1.0</tlib-version>
    <uri>http://example.com/functions&lt;/uri&gt;

    <function>
        <name>prettyURL</name>
        <function-class>com.example.Functions</function-class>
        <function-signature>java.lang.String prettyURL(java.lang.String)</function-signature>
    </function>
</taglib>

Which you can use in JSP as follows:

<%@taglib uri="http://example.com/functions" prefix="f" %>
...
<a href="teams/${contact.id}/${f:prettyURL(contact.name)}">Permalink</a>
BalusC
I didn't downvote you buddy
victor hugo
By the way, is there any doc regarding using - instead of + for spaces?
victor hugo
No, someone else did :) The downvote was a while after my update. The reason that the `-` is preferred is because the `+` is actually the "URL-safe" character for a space, not intended for SEO purposes. Look how Stackoverflow and another major sites do it.
BalusC
+1: It's a good answer now. Like it.
Donal Fellows
+2  A: 

Look for server.urlencode, all the major server side languages today have them.

SBR
This only applies [percent encoding](http://en.wikipedia.org/wiki/Percent-encoding). That's not really exactly what he's asking for. It would otherwise indeed have been a very straightforward answer :) It's by the way called `java.net.URLEncoder`. Not `server.urlencode` or something C-ish like that.
BalusC