Liferay: how to save to portlet user information?

2019-03-31 05:49发布

问题:

I have at the welcome page a weather portlet, and user can configure the portlet and select his city. Is it possible to store user information in the portlet preferences, so that every user has his one stored city? Or what is the standard workflow to store user-portlet information without to develop own (persist) service?

thx

回答1:

The portlet-preferences are in liferay per default not user specific. That can be modified in liferay-portlet.xml with next lines:

<liferay-portlet-app>
  <portlet>
    <portlet-name>ThePortletWitchUserSpecificPreferences</portlet-name>
    <icon>/icon.png</icon>
    <preferences-unique-per-layout>false</preferences-unique-per-layout>
    <preferences-owned-by-group>false</preferences-owned-by-group>
  </portlet>
...
</liferay-portlet-app>

the two lines <preferences-... and the order are abbreviated.

for more information see: http://rutvijshah.wordpress.com/2009/12/06/user-specific-preferences-in-liferay/



回答2:

It's not a native function of the PortletPreference : the setValue method allow only a String, unfortunately you can't pass a Map.

However, i see a solution to hardcode it, but it's a little bit ugly...

Long userId = ...... ; 
String userValue = ..... ;

PortletPreferences prefs = request.getPreferences();
prefs.setValue("myConfig-"+userId, myUserVal);
prefs.store();

And for retrieve the data :

String userValue = prefs.getValue("myConfig-"+userId, defaultValue);

This solution will work, but don't do that is you have a big numbers of users.
Portlet Preferences are save in xml in your database, if you have 100k+ users, it will explode :)

If you think this solution is not enough clean, you will have to create your own persistence method with the ServiceBuilder.