« Back to Development in...

Add a Properties File to a Portlet

Introduction #

The portal has a portal.properties file with values which configure the portal. When developing a portlet, you may also want a properties file to modify portlet configurations.

Easy Way #

  1. Create "portlet.properties" in portlet classpath (where all resources are), e.g. WEB-INF/src/portlet.properties;
  2. Use com.liferay.util.portlet.PortletProps.get(String key).

Note that if you're using Liferay classloader, portlet.properties file should go into /tomcat/webapps/ROOT/WEB-INF/classes.

Complex Way #

Create a PortletProps.java file #

To pull values from our own portlet properties file, we will create a file called "PortletProps.java" and in this file, we will specify the filename of our portlet properties file. Below, you will find the PortletProps.java file used for the mail portlet. Based on the code below, the properties file will be called mail-portlet.properties.

package com.liferay.mail.util;

import java.util.Properties;

import com.germinus.easyconf.ComponentProperties;
import com.liferay.util.ExtPropertiesLoader;

/**
 * <a href="PortletProps.java.html"><b><i>View Source</i></b></a>
 *
 * @author Scott Lee
 *
 */
public class PortletProps {

	public static boolean containsKey(String key) {
		return _getInstance().containsKey(key);
	}

	public static String get(String key) {
		return _getInstance().get(key);
	}

	public static void set(String key, String value) {
		_getInstance().set(key, value);
	}

	public static String[] getArray(String key) {
		return _getInstance().getArray(key);
	}

	public static Properties getProperties() {
		return _getInstance().getProperties();
	}

	public static ComponentProperties getComponentProperties() {
		return _getInstance().getComponentProperties();
	}

	private static ExtPropertiesLoader _getInstance() {
		return ExtPropertiesLoader.getInstance("mail-portlet");
	}

}

Add references to jar files in your **liferay-plugin-package.properties** file #

There is no need to add the physical jar files in your portlet. If your portlet uses jars which are already included in the portal, you only need to add them to the portal.dependency.jars list.

The following jars are required to add a properties file to your portlet.

    commons-beanutils.jar,\
    commons-collections.jar,\
    commons-configuration.jar,\
    commons-digester.jar,\
    easyconf.jar,\
    jasper-compiler-jdt.jar,\
    xstream.jar

Below is the liferay-plugin-package.properties file used for the mail portlet.

name=Mail
module-group-id=liferay
module-incremental-version=1
tags=
short-description=
change-log=
page-url=http://www.liferay.com
author=Liferay, Inc.
licenses=MIT

portal.dependency.jars=\
    commons-beanutils.jar,\
    commons-collections.jar,\
    commons-configuration.jar,\
    commons-digester.jar,\
    commons-lang.jar,\
    easyconf.jar,\
    jabsorb.jar,\
    jasper-compiler-jdt.jar,\
    jstl.jar,\
    jstl-impl.jar,\
    slf4j-log4j12.jar,\
    xstream.jar

portal.dependency.tlds=\
    c-rt.tld

Create your properties file #

We've created all the supporting code to read from a properties file, now we need to create the file itself. The actual filename will have to match the value that was specified in your PortletProps.java file.

The first line allows users of your portlet to extend/modify your properties file without actually modifying the original file:

include-and-override=mail-portlet-ext.properties

Below is the mail-portlet.properties file used for the mail portlet.

include-and-override=mail-portlet-ext.properties

root.dir=${resource.repositories.root}/mail
messages.to.prefetch=30
messages.per.page=25

Reading the values in your properties file #

Java #

import com.liferay.mail.util.PortletProps;

PortletProps.get("messages.per.page");

JSP #

<%@ page import="com.liferay.mail.util.PortletProps" %>

<%= PortletProps.get("messages.per.page") %>

Related Articles #

How To Articles

0 Attachments
51803 Views
Average (1 Vote)
The average rating is 2.0 stars out of 5.
Comments
Threaded Replies Author Date
When you follow the easy way of creating a... Rajkumar Balasubramanian October 27, 2009 3:19 PM
Sorry it is when creating a portlet properties... Rajkumar Balasubramanian October 27, 2009 3:20 PM
HI, While doing this inside a hook, I am... Smilelws2010 lwz February 10, 2011 1:15 PM
I'm experiencing the same problem..found the... Maarten J February 10, 2013 2:42 AM
If your problem is the last stacktrace: An... Josef Šustáček February 15, 2013 11:26 AM
Wow, that was easy! Thanks.. Dominik domih November 17, 2012 4:23 AM
The JSP code example still doesn't work, as... Ákos Gábriel March 18, 2013 10:53 AM
portal.depend_a_ncy should be portal.dependency... Ákos Gábriel March 18, 2013 11:27 AM

When you follow the easy way of creating a portlet url, the portlet.properties file has to be placed in the src folder.

WEB-INF/src/portlet.properties. This copies it to WEB-INF/classes/portlet.properties during the build.
Posted on 10/27/09 3:19 PM.
Sorry it is when creating a portlet properties file.
Posted on 10/27/09 3:20 PM in reply to rajkumar rajkumar.
HI,

While doing this inside a hook, I am getting the following error.

Stacktrace:
21:11:41,043 ERROR [IncludeTag:157] org.apache.jasper.JasperException: Unable to compile class for JS

An error occurred at line: 45 in the jsp file: /html/portlet/directory/view.jsp
com.liferay.util.portlet.PortletProp cannot be resolved to a type
42:
43:
44: %>
45: <%= com.liferay.util.portlet.PortletProp.get("Link1") %>
46:
47: <p style="text-align: center">
48: <span style="font-size: larger">


Stacktrace:
at org.apache.jasper.compiler.DefaultErrorHandler.javacError(DefaultErrorHandler.ja­va:92)
at org.apache.jasper.compiler.ErrorDispatcher.javacError(ErrorDispatcher.java:330)
at org.apache.jasper.compiler.JDTCompiler.generateClass(JDTCompiler.java:423)
at org.apache.jasper.compiler.Compiler.compile(Compiler.java:317)
at org.apache.jasper.compiler.Compiler.compile(Compiler.java:295)
at org.apache.jasper.compiler.Compiler.compile(Compiler.java:282)
at org.apache.jasper.JspCompilationContext.compile(JspCompilationContext.java:586)
at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:317)
at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:342)
at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:267)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilt­erChain.ja
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.­java:206)
at org.apache.catalina.core.ApplicationDispatcher.invoke(ApplicationDispatcher.java­:630)
at org.apache.catalina.core.ApplicationDispatcher.doInclude(ApplicationDispatcher.j­ava:535)
at org.apache.catalina.core.ApplicationDispatcher.include(ApplicationDispatcher.jav­a:472)
at com.liferay.taglib.util.IncludeTag.doEndTag(IncludeTag.java:66)
at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:374)

2­1:11:41,228 INFO [PluginPackageUtil:1340] Checking for available updates
21:11:41,230 INFO [PluginPackageUtil:1384] Finished checking for available updates in 1 ms


Can some one help?
Posted on 2/10/11 1:15 PM in reply to rajkumar rajkumar.
Wow, that was easy! Thanks..
Posted on 11/17/12 4:23 AM.
I'm experiencing the same problem..found the solution?
Posted on 2/10/13 2:42 AM in reply to An Jay.
If your problem is the last stacktrace:

An error occurred at line: 45 in the jsp file: /html/portlet/directory/view.jsp
com.liferay.util.portlet.PortletProp cannot be resolved to a type
42:
43:
44: %>
45: <%= com.liferay.util.portlet.PortletProp.get("Link1") %>
46:

There's a missing 's' ant the end of class name, it shoudl be PortletProps.
Posted on 2/15/13 11:26 AM in reply to Maarten J.
The JSP code example still doesn't work, as soon as I figure it out, I'll publish here.
Posted on 3/18/13 10:53 AM.
portal.depend_a_ncy should be portal.dependency I guess.
Posted on 3/18/13 11:27 AM.