« 返回到 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 附件
101786 查看
平均 (1 投票)
满分为 5,平均得分为 2.0。
评论
讨论主题回复 作者 日期
When you follow the easy way of creating a... Rajkumar Balasubramanian 2009年10月27日 下午3:19
Sorry it is when creating a portlet properties... Rajkumar Balasubramanian 2009年10月27日 下午3:20
HI, While doing this inside a hook, I am... Smilelws2010 lwz 2011年2月10日 下午1:15
I'm experiencing the same problem..found the... Maarten J 2013年2月10日 上午2:42
If your problem is the last stacktrace: An... Josef Sustacek 2013年2月15日 上午11:26
Wow, that was easy! Thanks.. Dominik Hofbauer 2012年11月17日 上午4:23
The JSP code example still doesn't work, as... Ákos Gábriel 2013年3月18日 上午10:53
portal.depend_a_ncy should be portal.dependency... Ákos Gábriel 2013年3月18日 上午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.
在 09-10-27 下午3:19 发帖。
Sorry it is when creating a portlet properties file.
在 09-10-27 下午3:20 发帖以回复 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?
在 11-2-10 下午1:15 发帖以回复 rajkumar rajkumar
Wow, that was easy! Thanks..
在 12-11-17 上午4:23 发帖。
I'm experiencing the same problem..found the solution?
在 13-2-10 上午2:42 发帖以回复 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.
在 13-2-15 上午11:26 发帖以回复 Maarten J
The JSP code example still doesn't work, as soon as I figure it out, I'll publish here.
在 13-3-18 上午10:53 发帖。
portal.depend_a_ncy should be portal.dependency I guess.
在 13-3-18 上午11:27 发帖。