Forums

Home » Liferay Portal » English » x. Miscellaneous

Combination View Flat View Tree View
Threads [ Previous | Next ]
toggle
Øyvind Askedal
Portlet reading from a custom properties file
August 15, 2012 12:41 AM
Answer

Øyvind Askedal

Rank: New Member

Posts: 2

Join Date: May 21, 2012

Recent Posts

Hi,

I'm new in Liferay and I'm trying to create a portlet that reads menu items from a properties file, as the menu items are most likely going to be all static datas. The portlet should then be integrated at the bottom in my theme. Any good tutorials or links on where to start?
My first guess was this, but I only get the message
\WEB-INF\classes\bottomMenu.properties (The system cannot find the path specified)

so I guess this is not the way?

Here is my code in the doView method of my portlet:

 1public void doView(RenderRequest renderRequest, RenderResponse renderResponse) throws IOException, PortletException {
 2       
 3        Properties properties = new Properties();
 4        String value="";
 5        try {
 6            String url = "/WEB-INF/classes/bottomMenu.properties";
 7            properties.load(new FileInputStream(url));
 8            
 9            for(String key : properties.stringPropertyNames()) {
10                value = properties.getProperty(key);
11                System.out.println(key + "=>" + value);
12            }
13            
14            renderRequest.setAttribute("url",value);
15            
16       
17        } catch (IOException e1) {
18            
19            System.out.println(e1);
20            
21        }
22       
23        //this.include(this.viewJSP, renderRequest, renderResponse);
24        super.doView(renderRequest, renderResponse);
25       
26    }
Vilmos Papp
RE: Portlet reading from a custom properties file
August 15, 2012 2:41 AM
Answer

Vilmos Papp

LIFERAY STAFF

Rank: Liferay Master

Posts: 521

Join Date: October 21, 2010

Recent Posts

Hi,

Please check the Mail Portlet, there you can see how to use portlet.properties for portlet specific property settings.

Regards,
Vilmos
Samuel Kong
RE: Portlet reading from a custom properties file
August 15, 2012 2:51 AM
Answer

Samuel Kong

LIFERAY STAFF

Rank: Liferay Legend

Posts: 1392

Join Date: March 10, 2008

Recent Posts

Probably a class loader issue. To avoid dealing with the class loader issue, just name your properties file, portlet.properties and place the file at /docroot/WEB-INF/src/portlet.properties Then you can just use the below code tor read your properties file:

1String value = com.liferay.util.portlet.PortletProps.get("my.property.key");
Hitoshi Ozawa
RE: Portlet reading from a custom properties file
August 15, 2012 4:18 AM
Answer

Hitoshi Ozawa

Rank: Liferay Legend

Posts: 7949

Join Date: March 23, 2010

Recent Posts

Following page contains a sample:

http://www.liferay.com/community/wiki/-/wiki/Main/How+to+add+a+Properties+File+to+a+Portlet
Ram A
RE: Portlet reading from a custom properties file
January 30, 2013 7:36 AM
Answer

Ram A

Rank: Junior Member

Posts: 76

Join Date: January 16, 2013

Recent Posts

Hi ,

Can anyone help me how to read the pom.properties file inside my portlets.
path : target\maven-archiver\pom.properties.

i think i have to read it as reading a file from classpath.



Thanks in advance
Ram A
David H Nebinger
RE: Portlet reading from a custom properties file
January 30, 2013 10:56 AM
Answer

David H Nebinger

Community Moderator

Rank: Liferay Legend

Posts: 11100

Join Date: September 1, 2006

Recent Posts

ClassLoader.getResourceAsStream("target/maven-archiver/pom.properties") to get the input stream. Route to a new Properties instance (has loading from input stream), where you can fetch the property.
K. Behrends
RE: Portlet reading from a custom properties file
February 20, 2015 5:40 AM
Answer

K. Behrends

Rank: New Member

Posts: 4

Join Date: January 6, 2012

Recent Posts

You can read the base.path property to get the path to the portlet.properties file, and then work your way to your custom file.

Properties props = loadProperties(); //liferay API


String basepath = props.getProperty("base.path");
Properties customprops = readPropfile(basepath);


and then

public Properties readPropfile(String defaultpropfilename){

Properties props = new Properties();
InputStream is = null;
File file = new File(defaultpropfilename);
String parentDirName = file.getParent();

// try loading from the same directory
try {
File f = new File(parentDirName, "bottomMenu.properties");
is = new FileInputStream( f );
props.load( is );
is.close();
}
catch ( Exception e ) { is = null;
e.printStackTrace();

}

return props;

}
Tariqul Islam
RE: Portlet reading from a custom properties file
February 27, 2015 5:49 AM
Answer

Tariqul Islam

Rank: New Member

Posts: 15

Join Date: January 27, 2013

Recent Posts