Liferay: how to save to portlet user information?

扶醉桌前 提交于 2019-12-09 13:24:58

问题


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.



来源:https://stackoverflow.com/questions/7015508/liferay-how-to-save-to-portlet-user-information

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