« Voltar para 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 Anexos
101795 Visualizações
Média (1 Votar)
A média da avaliação é 2.0 estrelas de 5.
Comentários
Respostas do tópico Autor Data
When you follow the easy way of creating a... Rajkumar Balasubramanian 27 de Outubro de 2009 15:19
Sorry it is when creating a portlet properties... Rajkumar Balasubramanian 27 de Outubro de 2009 15:20
HI, While doing this inside a hook, I am... Smilelws2010 lwz 10 de Fevereiro de 2011 13:15
I'm experiencing the same problem..found the... Maarten J 10 de Fevereiro de 2013 02:42
If your problem is the last stacktrace: An... Josef Sustacek 15 de Fevereiro de 2013 11:26
Wow, that was easy! Thanks.. Dominik Hofbauer 17 de Novembro de 2012 04:23
The JSP code example still doesn't work, as... Ákos Gábriel 18 de Março de 2013 10:53
portal.depend_a_ncy should be portal.dependency... Ákos Gábriel 18 de Março de 2013 11:27

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.
Postado em 27/10/09 15:19.
Sorry it is when creating a portlet properties file.
Postado em 27/10/09 15:20 em resposta a 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?
Postado em 10/02/11 13:15 em resposta a rajkumar rajkumar.
Wow, that was easy! Thanks..
Postado em 17/11/12 04:23.
I'm experiencing the same problem..found the solution?
Postado em 10/02/13 02:42 em resposta a 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.
Postado em 15/02/13 11:26 em resposta a Maarten J.
The JSP code example still doesn't work, as soon as I figure it out, I'll publish here.
Postado em 18/03/13 10:53.
portal.depend_a_ncy should be portal.dependency I guess.
Postado em 18/03/13 11:27.