Requires namespaced Parameter in Liferay

Liferay requires namespaced parameter starting from version 6.2. This is required to avoid name collision.

requires namespaced

While working with portlet, most of the cases you are submitting some values from jsp, taking it in action or render method and perform some operation on it.

Many times, even if you pass the parameters from JSP, you are getting null or empty values for those parameter in portlet class. In this article, I will be exploring the reason behind it.

We will understand it by creating liferay MVC portlet. I gave project name as required-namespace and -portlet will be appended by Liferay IDE while creating project. I created portlet class as com.opensource.techblog.portlet.RequiredNamespacePortlet. The project structure should look like below screenshot at this point.

requires namespaced - Project Structure

Add following code in view.jsp (vide.jsp is created under docroot folder by Liferay IDE)

Explanation

  • I have created action url and created one html form and assigned it’s action attribute to actionURL.
  • In this form there is just one input element called param1.
  • When user submit this form, the value of param1 parameter should be available in action method.

Add action method as per following code in portlet class – RequiredNamespacePortlet.java

Explanation

  • I have defined one action method. This action method will be called when user submit html form in view.jsp.
  • In this action method, I am simply reading request parameter that is passed from view.jsp and printing it in log.
  • I have used ParamUtil to read request parameter. You may use request.getParameter() method to read request parameter.
  • If request parameter is not available then ParamUtil.get method will return default value (3rd parameter) while request.getParameter() will return null.

Save these file, deploy and place the portlet on page. You will see one text box and one submit button. Give some value in text box and submit the form. You will get empty / null value of this value in action method.

We took example of action method but if you read request parameter in render or resource method then also you will get null / empty value for request parameter.

Problem statement:- getting null / empty value of request parameter in action / render or resource method.

Reason: staring from version 6.2, Liferay reads the request parameter only if you have attached the portlet namespace to it. This is default behavior. Liferay requires namespaced parameter. It means liferay by default enforce you to pass portlet namespace along with parameter name. In above example, we haven’t pass portlet namespace so portlet will not read the request parameter.

before further explanation, you must understand what is portlet namespace. Portlet namespace is an unique value assigned by portal to each portlet on page.

How to solve requires namespaced issue

There are various solutions to read request parameters (especially starting from liferay 6.2) so that requires namespaced criteria is fulfilled.

By Passing portlet namespace to each form element

  • This is very straight forward. You must have to pass portlet namespace to each form element / request parameter. You may refer my blog on portlet namespace usage. Portlet namespace can be passed by following ways.

OR

  • Pros
    • Easy to use, no extra setting required. This is simplest way to fulfill requires namespaced parameter.
    • Enforcing portlet namespace will save name collision at later stage of development.
  • Cons
    • If you already written lots of JSP code with many form elements, then you need to put extra effort to pass portlet namespace to each form element in each JSP. This is applicable especially you are migrating your code from liferay 6.1 to 6.2

Set <requires-namespaced-parameters> to false

  • This is another way to read parameter in portlet.
  • In this approach, we will Instruct liferay not to enforce us to pass portlet namespace. This can be achieved by setting <requires-namespaced-parameters> to false in liferay-portlet.xml file
  • In other words, requires namespaced is a switch for that portlet. Default value for requires namespaced parameter is true. If requires namespaced parameter is set to false then portal will allow to read all parameter (with or without portlet namespace) for that portlet.
  • Pros
    • Easy to use.
    • In case if you are migrating your existing code from liferay 6.1 to 6.2, it’s just one place configuration to read parameters.
    • Parameters are not requires namespaced.
  • Cons
    • It’s not mandatory to pass namespace in this approach. This means there is a chance of name collision at later stage.
    • Only choose this approach if you are sure about name collision.

Use AUI tags

  • Liferay provides AUI taglibs to construct various form elements.
  • When you use AUI taglibs, it will automatically pass portlet namespace to that element.
  • Below code snippet shows how to use AUI element.

  • To avail AUI taglib, you must have to define <% taglib uri=”http://alloy.liferay.com/tld/aui” prefix=”aui” %> in your JSP.
  • when this tag resolve, it will generate input element like below snippet

  • As you can observed that the portlet namespace (_requirednamespace_WAR_requirednamespaceportlet_) is attached to studentName field name when this tag is resolved.
  • For complete list of AUI form element, you can refer AUI form element link.
  • Pros
    • Easy to use.
    • No worry about passing portlet namespace as it will do automatically.
    • The need of requires namespaced criteria is fulfilled.
  • Cons
    • New Learning.
    • AUI form elements can’t be used in certain situation for example you had your own theme and htmls.

Summing Up

  • To avoid name collision, liferay requires namespaced parameter staring from version 6.2.
  • It’s always best practice to pass portlet namespace to each form element to fulfill requires namespaced criteria.
  • Liferay however provides few ways to either pass portlet namespace automatically (use AUI taglib) or not enforcing us to pass portlet namespace (by setting requires-namespaced-parameters to false in liferay-portlet.xml).

Share This Post

Post Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.