How to get init param from portlet.xml in liferay?

I am using a custom porltet in my portal liferay 6.i has some global variable that I want to use through my portlet classes, so for that I wrote this variable in the portlet.xml file as follows.

<init-param>
        <name>Host</name>
        <value>smtp.mail.yahoo.com</value>
</init-param>

<init-param>
        <name>Port</name>
        <value>465</value>
</init-param>

      

which works fine in the portlet action class

publicList<String> ReadSmtpDataForMailNotification() {

        List<String> ValuesListObj = new ArrayList<String>();
        ValuesListObj.add(this.getInitParameter("Host"));
        ValuesListObj.add(this.getInitParameter("Port"));
        return ValuesListObj;
    }

      

now the problem is that when I clean out the function with the portlet action class then its work fine, but when I want to access this variable outside of my portlet class..eg: - in my local service class then I am not I can access this variable and the value is always null..so please if anyone can suggest me how I can get the initparam value in another portlet class.

@ Advaita Goswami

wrote file my.custom.host = liferay.css.com in portlet.properties file

and when i try to get the value with the following

 System.out.println("Property value : - " + PropsUtil.get("my.custom.host"));

      

giving me a null value.

+3


source to share


3 answers


Anyway, I did it as @Advaita Gosvami said earlier with changes in portlet.properties, but some how it doesn't work with my custom key ...

mail.session.mail.smtps.auth=true
mail.session.mail.smtps.host=smtp.gmail.com
mail­.session.mail.smtps.password=***********(your password here)
mail.session.mail.smtps.port=465
mail.session.mail.s­mtps.user=liferay.test@gmail.com(your emailId Here)
mail.session.mail.transport.protocol=smtp

      

and i call it with



PortletProps.get("mail.session.mail.smtps.host");

      

it gave me perfect value from anywhere i call in my custom portlet

Thanx @Advaita Gosvami for your useful info help.just in case u decalre custom key in portlet properties givng me null value.but with these keys as i mentioned above in portlet properties i can access ... thnx a lot

0


source


The variables in <init-param>

are for accessing the portlet itself, not any other class. These options do not apply to all other classes.

So, if you want to access parameters in other classes than passing parameters as arguments to the class method (here local service impl) from the portlet class.

Another way I would like to suggest is to have these parameters in the file portal-ext.properties

, for example:

my.custom.host=smtp.mail.yahoo.com
my.custom.port=465

      

And then enter it from that file using PropsUtil

. With this approach, these properties will be available to all portlets in this portal.



You can also put the above values ​​in portlet.properties

and can be accessed via case PortletProps

. With this approach, it will only be available for a single portlet.

Or you can even try to use these parameters inside the Constant class, for example:

interface MyConstants {
    String HOST = "smtp.mail.yahoo.com";
    String PORT = "465";
}

      

This second method is best if you only want it for one portlet.

I would suggest thinking about why you need these parameters in <init-param>

.

+3


source


Annotation:

If you want to get the initialization parameters of a portlet at runtime, you can use yours com.liferay.portal.service.PortletLocalServiceUtil

to get your portlet.

Then you will receive an instance com.liferay.portal.model.Portlet

.

It is a very convenient object because it offers you the ability to call a convenience method:

public java.util.Map<java.lang.String, java.lang.String> getInitParams();

      

Snippets of code:

In a portlet, a servlet running on your liferay server, a hook running on server startup, a quartz job, a service ...

Can call the following code to get the portlet paralet initialization map

import com.liferay.portal.model.Portlet;
import com.liferay.portal.service.PortletLocalServiceUtil;
import java.lang.String;
import java.util.Map;

...

/* Determine what is your portlet id, you can go as follows or find it after placing one in a page and check it id with a tool like firebug */
String myPortletId = "myPortletName_WAR_myPortletWar";

/* Get the actual portlet model object with the appropriate service call */
Portlet myPortletObject PortletLocalServiceUtil.getPortletById(myPortletId);

/* Get the map of init-params from the portlet model object */
Map<String,String> initParamMap = myPortletObject.getInitParams();

/* You can now iterate on your map as on any other */
for (String currentParamKey : initParamMap.keySet()) {
    String currentParamValue = initParamMap.get(currentParamKey);
    /* do stuff */
}

      

Principles behind the scenes:

In Liferay, the portlet must be registered for use.
When deployed, the portal registers it, registers it, init-params

and a bunch of other information .

What's most fortunate about this is that Liferay will store all of this data in a database. Like almost all of the information that Liferay stores in a database, this information is available through the LiferayService.

These services come with a bunch of useful methods.

  • Some very simple ones (like the advanced cruds)
  • More specifically for the current service.

In my example code, I got my portlet object thanks to the portlet search method on portletId

. This is portletId

not the primary key of the table database Portlet

.
Hence, Liferay is dealing with concatenating portletId

(actually, that name) instead of forcing a primary key query, which is numeric meaningless long

.

You can use other methods to get your portlet object as PortletLocalServiceUtil has many search engines.
I highly recommend that you check the file $LIFERAY_PORTAL_SRC/portal-service/src/com/liferay/portal/service/PortletLocalServiceUtil.java

in any IDE that offers an outline view to get a better understanding of all of its capabilities.

Output:

Hope it helps;)

+3


source







All Articles