1

Portlets have an EDIT mode, and this is the way we can parametrize them.

My question is what it the best way to parametrize a portlet exactly at the moment its loaded, programmatically.

For example: I want to get Liferay to load two instances of the portlet with a certain ID, one with paramA, second with paramB.

Community
  • 1
  • 1
shabunc
  • 23,119
  • 19
  • 77
  • 102
  • 2
    I always used one of two approach: either all portlet instances could have the *same* default preference values (case when I see if there is some preference saved and use the default ones if there is none) or I add the instances to the portal and configure them with the edit mode. Your idea is feasible with some creativity, but I would suggest you to post the real, high level problem you are trying to solve because your idea seems a bit odd and risky. – brandizzi Oct 04 '11 at 13:47
  • @brandizzi - imagine any data viewer which deals with any homogenous data. for example, user cards. It seems very logical to try to implement such viewer as a portlet which is parametrized. – shabunc Oct 04 '11 at 14:02
  • Sorry, I could not understand your comment. What are these user cards? From where is this homogeneous data retrieved? – brandizzi Oct 04 '11 at 14:17
  • @brandizzi, user cards is just example. In the question I've linked there is another example - imagine map portlet which can be centered with some predefined values. – shabunc Oct 04 '11 at 14:20

3 Answers3

2

To load different parameters (better known in Liferay as "Portlet Preferences") you can use PortletPreferences to store and retrieve different parameters,

PortletPreferences preferences =
    PortletPreferencesFactoryUtil.getPortletSetup(
        request, portletId);

The factory takes 2 parameters,

  • Request - The request.
  • PortletId - The ID of your portlet, (for example "name_WAR_myportlet_INSTANCE_ABCD").

With this object you can get parameters:

String myValue = preferences.getValue("my-value");

And you can store values:

preferences.setValue("my-value", "this-value");
preferences.store();
Dani
  • 3,744
  • 4
  • 27
  • 35
rp.
  • 3,435
  • 1
  • 21
  • 29
1

"Liferay embed portlets" gives a good basic search on what I interpret your question to ask for (but it's not fully clear to me).

With this I find hits like Embedding portlets in your portlet and Embedding Portlets in Web Content that seem to match.

If it's not that I second brandizzi's comment: Please give the high level problem (that you need to solve in business terms) instead of the proposed solution (that you need the technical details for, even if it's a bad solution for the original problem)

Olaf Kock
  • 46,930
  • 8
  • 59
  • 90
0

You could use url parameters.

I'll describe two approaches.

1) Use different parameters for each portlet, but you have to configure each portlet (portlet preferences) with parameter name which to look for (different for each portlet).

final String parameter = p_request.getPreferences().getValue("parameterName", null));

javax.servlet.http.HttpServletRequest request = com.liferay.portal.util.PortalUtil.getOriginalServletRequest(com.liferay.portal.util.PortalUtil.getHttpServletRequest(p_request));
final String paramValue = request.getParameter(parameter);


usage example: http://www.myserver.com/mypagewithportlets?param1=something1&param2=somethnig2


2) Use parameter prefixed with portlet namespace, but when using parameters you need to know portlet id's (napespace is portlet id with prefix=suffix=_)

javax.servlet.http.HttpServletRequest request = com.liferay.portal.util.PortalUtil.getOriginalServletRequest(com.liferay.portal.util.PortalUtil.getHttpServletRequest(p_request));
final String paramValue = request.getParameter(p_response.getNamespace() + "myparameter");


usage example: http://www.myserver.com/mypagewithportlets?_name_WAR_myportlet_INSTANCE_xzy1_myparameter=something1&_name_WAR_myportlet_INSTANCE_syhs_myparameter=somethnig2


Just a note
p_request implements javax.portlet.PortletRequest
p_response implements javax.portlet.PortletResponse

Dani
  • 3,744
  • 4
  • 27
  • 35
Martin Gamulin
  • 3,855
  • 21
  • 25