Friendlier Friendly URL Mapper for Liferay 6

It’s been a while since I written a post, but last Thursday I was working on Liferay portlet which involved using Liferay’s built-in Friendly URL Mapping functionality, and I hit a small snag which I thought I’d write about now.

I’m going to assume that you already know about Friendly URL Mapper in Liferay 6. If not then you should take a look at the Liferay Wiki page on it, or get the great Liferay In Action book, then come back here!

The Friendly URL Mapper is great. It’s quick and simple to set up, and hides a lot of the complexities of creating friendly URLs in Liferay. However it has one characteristic that I had to get rid of for my portlet. This was the Friendly URL Mapper prefix! If you have a look at the URL below between the two mentions of /wiki/ you’ll see what I’m talking about:

http://www.liferay.com/community/wiki/-/wiki/Main/FriendlyURLMapper

That /-/ is the Friendly URL Mapper prefix, and to my mind looks really bad! However I believe the reason behind it’s existence is also shown in the above URL. The prefix signifies the beginning of the Friendly URL, and the end of the actual URL. So you can have a page named the same as the as name of the mapping (as you can see the page above is “wiki” and the mapping is also “wiki”. With the code I’m showing you below, this won’t work so bare that in mind!

So there are two changes you need to make to your existing Friendly URL Mapping code to remove that prefix. The first is to create a new a class along the lines of MyPortletNameFriendlyURLMapper. This class needs to extend com.liferay.portal.kernel.portlet.DefaultFriendlyURLMapper. Then you all you need to do is override the isCheckMappingWithPrefix method of the BaseFriendlyURLMapper, by returning false. The code for this class can be seen below:

package com.mysmallcornerstudios.friendlierurlmapping.portlet;

import com.liferay.portal.kernel.portlet.DefaultFriendlyURLMapper;

public class FriendlierFriendlyURLMapper extends DefaultFriendlyURLMapper {

@Override
public boolean isCheckMappingWithPrefix() {

return false;

}

}

Then all we have to do is update the liferay-portlet.xml to say use our new FriendlyURLMapper class instead of the Default one, but the Default one is still doing most of the work for us. So in my liferay-portlet.xml I just change the <friendly-url-mapper-class> to the below:

<friendly-url-mapper-class>

com.mysmallcornerstudios.friendlierurlmapping.portlet.FriendlierFriendlyURLMapper

</friendly-url-mapper-class>

That’s it! Job done! So an example of what we’ve done here is change this URL:

http://mysite.com/somepage/-/friendlier/someParameter/

to:

http://mysite.com/somepage/friendlier/someParameter/

So it’s even friendlier to look at! The source code for this can be found in my GitHub repository called Liferay-6-Friendlier-Friendly-URL-Mapper. I’ve also added a WAR file to my repository so you can easily deploy this portlet. The WAR file can be found here.

I hope this helps people!

Comments (4)

  1. 6:37 am, December 22, 2011Mark  / Reply

    Thank you. This was helpful.

    • 7:31 am, December 22, 2011DevJonny  / Reply

      Thanks Mark. If there’s anything else you’d like to see posted, let me know.

  2. 10:52 am, February 20, 2012Olivier ROMAND  / Reply

    Hello, thanks for the post.

    Could you provide source code of the implementation as well ?
    Thanks in advance !

    • 11:05 am, February 20, 2012DevJonny  / Reply

      Hi Olivier,

      When you say source code for the implmentation, what do you want to see? What I have posted is basically all the code you need. Anything else would be standard Liferay portlet code + your own code for the needs of the portlet. Let me know what you need!

Leave a Reply

Allowed Tags - You may use these HTML tags and attributes in your comment.

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Pingbacks (1)