留言板

Portlet reading from a custom properties file

Øyvind Askedal,修改在11 年前。

Portlet reading from a custom properties file

New Member 帖子: 2 加入日期: 12-5-21 最近的帖子
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,修改在11 年前。

RE: Portlet reading from a custom properties file

Liferay Master 帖子: 529 加入日期: 10-10-21 最近的帖子
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,修改在11 年前。

RE: Portlet reading from a custom properties file

Liferay Legend 帖子: 1902 加入日期: 08-3-10 最近的帖子
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,修改在11 年前。

RE: Portlet reading from a custom properties file

Liferay Legend 帖子: 7942 加入日期: 10-3-24 最近的帖子
Following page contains a sample:

http://www.liferay.com/community/wiki/-/wiki/Main/How+to+add+a+Properties+File+to+a+Portlet
Ram A,修改在11 年前。

RE: Portlet reading from a custom properties file

Junior Member 帖子: 76 加入日期: 13-1-16 最近的帖子
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,修改在11 年前。

RE: Portlet reading from a custom properties file

Liferay Legend 帖子: 14915 加入日期: 06-9-2 最近的帖子
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,修改在9 年前。

RE: Portlet reading from a custom properties file

New Member 帖子: 4 加入日期: 12-1-6 最近的帖子
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,修改在9 年前。

RE: Portlet reading from a custom properties file

New Member 帖子: 15 加入日期: 13-1-27 最近的帖子