How can you create MVC URLs?

Render and action URLs in Java

More often than one would wish we find ourselves trying to remember the API we need to create URLs out of a JSP, and some will end up finding ways to leave this task to a JSP somehow, even when they need it in several JSP files or a JSP tag requires a URL object  not accepting Strings (more on that later), among other cases.

Let’s start simple, in a JSP it is trivial to have a piece of code like the following:

<portlet:renderURL var='pullRenderUrl'>
   <portlet:param name='mvcRenderCommandName' value='pull'/>

This is fine, but what if we need to reuse it in other parts? One could copy the snipped or create common fragments, such as the init.jspf . OK not a big deal, but if you have to use objects not available in your JSP, you would need to pass them along just to create a URL.

Imagine when you are in a Tomcat setup, like 8.0, that does not allow the use of static methods and you need that method, then you create a bean to pass that data along to the JSP level… well cases vary a lot, but the truth is: we do need to be able to create URLs for our portlets and sometimes before we reach the JSPs.

Here is an interesting case, you need to use a tag, which will only accept a PortletURL object and your style guide demands the use of EL, instead of scriptlets. The tag that generates the URL is definitely able to generate a variable with the object for us, if we use the Liferay specific tag, and we ask it to put in a varImpl, instead of var. But then, your IDE starts complaining that it is not able to validate… Here we have an example of a tag that needs the object as we mentioned.

<liferay-frontend:edit-form method="post" name="fm" action="${actionUrl}" markupView="lexicon">

It doesn’t matter the case, if you are here, you are probably looking for something like this:

import static javax.portlet.PortletRequest.RENDER_PHASE;

private static PortletURL getRenderUrl( PortletRequest request ) {

   PortletURL portletURL = PortletURLFactoryUtil.create( request, EXAMPLE_PORTLET, RENDER_PHASE );
   portletURL.setParameter( "mvcRenderCommandName", RENDER_COMMAND_EXAMPLE );

   return portletURL;

This example shows a simple way to create a render URL for you MVC command and the following shows the same for an action command.

import static javax.portlet.ActionRequest.ACTION_NAME;
import static javax.portlet.PortletRequest.ACTION_PHASE;

private void setPullActionUrl( PortletRequest request ) {

   PortletURL portletURL = PortletURLFactoryUtil.create( request, EXAMPLE_PORTLET, ACTION_PHASE );

   portletURL.setParameter( ACTION_NAME, ACTION_COMMAND_PULL );
   portletURL.setParameter( "mvcRenderCommandName", RENDER_COMMAND_PULL );

   request.setAttribute( "pullActionUrl", portletURL );

If your IDE complains you can always use the good old jsp:useBean, or any IDE exclusive method, like IDEA's especial comments to declare an attribute's type for the purpose of validation when editing.

<%--@elvariable id="pullActionUrl" type="javax.portlet.PortletURL"--%>

<jsp:useBean id="pullActionUrl" scope="request" type="javax.portlet.PortletURL"/>

There you go, you do not need to set all those ugly parameters we normally see around the Internet, it can be as simple as the tag we normally use in JSPs, unless you need something specific.
Good coding!

More Blog Entries