Vue combinée Vue Plate Arborescence
Sujets [ Précédent | Suivant ]
toggle
Christophe Noel
Best practices for accessing context parameters from beans
22 janvier 2014 05:29
Réponse

Christophe Noel

Rang: Regular Member

Publications: 100

Date d'inscription: 28 septembre 2012

Publications Récentes

Hello,

It's been some time that, from my Managed Bean, I have been using the same JSF approach in order to access parameters such as portletId, portletInstanceId, groupId, etc.
I would like to know if there is a better and shorter way for accessing this data ? (because it seems quite tricky isn't it ?)

My current approach is as follow:

Get ThemeDisplay :
1
2ExternalContext context = FacesContext.getCurrentInstance().getExternalContext(); // or LiferayFacesContext works too
3ThemeDisplay themeDisplay = (ThemeDisplay) context.getRequestMap().get(WebKeys.THEME_DISPLAY);


Get Portlet Id:
1
2String portletId = themeDisplay.getPortletDisplay().getId();


Thanks for your advice.

Christophe.
David H Nebinger
RE: Best practices for accessing context parameters from beans
22 janvier 2014 05:40
Réponse

David H Nebinger

Community Moderator

Rang: Liferay Legend

Publications: 11085

Date d'inscription: 1 septembre 2006

Publications Récentes

Best practice - don't do it.

Seriously, if you've made yourself dependent upon the context params, especially the portlet id, I think you've done something wrong. I've written many portlets in jsp/struts, jsf, and vaadin and not one has been dependent upon context params, and never would I base dependence on the portlet id.
Christophe Noel
RE: Best practices for accessing context parameters from beans
22 janvier 2014 06:53
Réponse

Christophe Noel

Rang: Regular Member

Publications: 100

Date d'inscription: 28 septembre 2012

Publications Récentes

What do you mean ?

When developing Liferay services, I generally use the serviceContext (to allow permissions on specific groupId, companyId).
When setting preferences per portlet instance, I would need the portlet instance Id.

So what am I'm doing wrong ?
Vernon Singleton
RE: Best practices for accessing context parameters from beans
22 janvier 2014 07:24
Réponse

Vernon Singleton

LIFERAY STAFF

Rang: Expert

Publications: 278

Date d'inscription: 14 janvier 2013

Publications Récentes

Christophe Noel:
I would like to know if there is a better and shorter way for accessing this data ? (because it seems quite tricky isn't it ?)

My current approach is as follow:

Get ThemeDisplay :
1
2ExternalContext context = FacesContext.getCurrentInstance().getExternalContext(); // or LiferayFacesContext works too
3ThemeDisplay themeDisplay = (ThemeDisplay) context.getRequestMap().get(WebKeys.THEME_DISPLAY);


Hi Christophe,

You might like:
1
2LiferayFacesContext.getInstance().getThemeDisplay()
3

There are all kinds of handy Liferay specific methods hanging off of the LiferayFacesContext.
Hope you like it.

- Vernon
David H Nebinger
RE: Best practices for accessing context parameters from beans
22 janvier 2014 08:41
Réponse

David H Nebinger

Community Moderator

Rang: Liferay Legend

Publications: 11085

Date d'inscription: 1 septembre 2006

Publications Récentes

portlet instance id is not used in the service context.

company and group ids are necessary if you are storing records that have company/group access limitations, but those values can be accessed through other means (I believe there's a thread local to get to these details, kinda like the PrincipalThreadLocal).
Christophe Noel
RE: Best practices for accessing context parameters from beans
28 janvier 2014 02:39
Réponse

Christophe Noel

Rang: Regular Member

Publications: 100

Date d'inscription: 28 septembre 2012

Publications Récentes

Vernon Singleton:

Hi Christophe,
You might like:
1
2LiferayFacesContext.getInstance().getThemeDisplay()
3

There are all kinds of handy Liferay specific methods hanging off of the LiferayFacesContext.
Hope you like it.
- Vernon


Thanks Vernon, this is very useful to know.

Therefore, is the "portlet custom title" also simplified with Liferay Faces ? I may only get this value as follow:
1ThemeDisplay themeDisplay = LiferayFacesContext.getInstance()
2                .getThemeDisplay();
3        String portletId = themeDisplay.getPortletDisplay().getId();
4        javax.portlet.PortletPreferences portletSetup = PortletPreferencesFactoryUtil
5                .getLayoutPortletSetup(themeDisplay.getLayout(), portletId);
6        String portletCustomTitle = themeDisplay.getPortletDisplay().getTitle();
7        portletCustomTitle = portletSetup.getValue("portletSetupTitle_"
8                + themeDisplay.getLanguageId(), portletCustomTitle);


It seems that liferay.portlet.displayName or liferay.themeDisplay.portletDisplay.title are still not rewritten at the right time.

Thanks, Christophe.