Fórum

Portlet reading from a custom properties file

Øyvind Askedal, modificado 11 Anos atrás.

Portlet reading from a custom properties file

New Member Postagens: 2 Data de Entrada: 21/05/12 Postagens Recentes
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:

public void doView(RenderRequest renderRequest, RenderResponse renderResponse) throws IOException, PortletException {
		
		Properties properties = new Properties();
		String value="";
		try {
			String url = "/WEB-INF/classes/bottomMenu.properties";
			properties.load(new FileInputStream(url));
			
			for(String key : properties.stringPropertyNames()) {
				value = properties.getProperty(key);
				System.out.println(key + "=>" + value);
			}
			
			renderRequest.setAttribute("url",value);
			
		
		} catch (IOException e1) {
			
			System.out.println(e1);
			
		}
		
		//this.include(this.viewJSP, renderRequest, renderResponse);
		super.doView(renderRequest, renderResponse);
		
	}
thumbnail
Vilmos Papp, modificado 11 Anos atrás.

RE: Portlet reading from a custom properties file

Liferay Master Postagens: 529 Data de Entrada: 21/10/10 Postagens Recentes
Hi,

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

Regards,
Vilmos
thumbnail
Samuel Kong, modificado 11 Anos atrás.

RE: Portlet reading from a custom properties file

Liferay Legend Postagens: 1902 Data de Entrada: 10/03/08 Postagens Recentes
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:

String value = com.liferay.util.portlet.PortletProps.get("my.property.key");
thumbnail
Hitoshi Ozawa, modificado 11 Anos atrás.

RE: Portlet reading from a custom properties file

Liferay Legend Postagens: 7942 Data de Entrada: 24/03/10 Postagens Recentes
Following page contains a sample:

http://www.liferay.com/community/wiki/-/wiki/Main/How+to+add+a+Properties+File+to+a+Portlet
Ram A, modificado 11 Anos atrás.

RE: Portlet reading from a custom properties file

Junior Member Postagens: 76 Data de Entrada: 16/01/13 Postagens Recentes
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
thumbnail
David H Nebinger, modificado 11 Anos atrás.

RE: Portlet reading from a custom properties file

Liferay Legend Postagens: 14914 Data de Entrada: 02/09/06 Postagens Recentes
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, modificado 9 Anos atrás.

RE: Portlet reading from a custom properties file

New Member Postagens: 4 Data de Entrada: 06/01/12 Postagens Recentes
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;

}
thumbnail
Tariqul Islam, modificado 9 Anos atrás.

RE: Portlet reading from a custom properties file

New Member Postagens: 15 Data de Entrada: 27/01/13 Postagens Recentes