« Back to Development in the...

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 porlet classpath (where all resources are);
  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.dependancy.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
20418 Views
Average (1 Vote)
Comments

Showing 3 Comments

Rajkumar Balasubramanian
10/27/09 3:19 PM

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.

Rajkumar Balasubramanian
10/27/09 3:20 PM

Sorry it is when creating a portlet properties file.

Smilelws2010 lwz
2/10/11 1:15 PM

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?