
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 #
- Create "portlet.properties" in portlet classpath (where all resources are), e.g. WEB-INF/src/portlet.properties;
- 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") %>