Liferay: how to save to portlet user information?

。_饼干妹妹 提交于 2019-12-03 16:42:39

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/

Julien Lafont

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!