Question

i am using custom porltet in my liferay portal 6.i have some global variable that i want to use through out my portlet classes so for that i have written that variable in 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>

function which working perfect in my 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 problem is that when i excute the function withing the portlet action class then its working perfect but when i want to access this variable outside my portlet class..e.g :- in my local service impl class then i cant access that varible and the value come is always null..so please if anyone can suggest me how can i do get the value of initparam in other then portlet class.

@Advaita Gosvami

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

and when i try to fetch value with the following

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

its giving me null value..

Était-ce utile?

La solution 3

Anyways I Have Done like this as @Advaita Gosvami told before with the changes in portlet.properties but some how it doesnt 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 am calling it with

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

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

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

Autres conseils

Abstract :

If you want to get a portlet's init-params at runtime, you may use your com.liferay.portal.service.PortletLocalServiceUtil to get your portlet.

Then you get an instance of com.liferay.portal.model.Portlet.

This is a very convenient object as it offers you the possibility to call a handy method :

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

Code Snippets :

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

May call the following code to obtain a portlet's init-param 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's id, you can go as follows or find it after placing one in a page and check it's 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 scene :

In Liferay, a portlet needs to be registered to be usable.
When deployed, the portal registers it, registers it's init-params and a bunch of other informations.

What's most fortunate about this very fact is that Liferay will thus store all those datas in it's database.
Like pretty much all informations that Liferay stores in database, those informations are accessible through a LiferayService.

Those services are shipped with a bunch of utility methods.

  • Some, very basic (like an advanced crud)
  • Some more specific to the current service.

In my code example i got my portlet object thanks to a method that search portlets by portletId. This portletId is not the database primary key of the table Portlet.
Hence, Liferay does it's own business to relate the portletId (in reality, it's name) instead of forcedly asking the primary key which is a numeric meaningless long.

You may use other methods to get your portlet's object as PortletLocalServiceUtil has many finders available.
I strongly recommend 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 idea of all it's possibilities.

Conclusion :

Hope this helps ;)

The variables in <init-param> are meant to be accessed by the portlet itself and not by any other class. These parameters are not for all the other classes.

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

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

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

And then access it from this file using PropsUtil. With this approach these properties would be available to all the portlets of this portal.

You can also put the above values in the portlet.properties and can access via PortletProps case. With this approach it would be available for the individual portlet only.

Or else you can even try to have these parameters inside a Constant class like:

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

This second way would be the best, if you want it just for one portlet.

I would suggest think about why do you need these parameters in <init-param>.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top