Reputation: 1115
I have problems using the PortletPreferences in my portlet. The doView(...)
method shows the normal view.jsp or a custom jsp called controlpanel.jsp depending on the page id (controlpanel.jsp shows up in Liferays ControlPanel, view.jsp anywhere else).
I added a form in the controlpanel-page, where user can type in database-properties. When the user submits the form, the data will be processed and saved if not empty. Here's my action-method:
@ProcessAction(name="saveControlPanelSettings")
public void saveControlPanelSettings(ActionRequest actionRequest,
ActionResponse actionResponse) throws ReadOnlyException, ValidatorException, IOException {
String host, port, user, pass;
host = ParamUtil.get(actionRequest, "host", StringPool.BLANK);
port = ParamUtil.get(actionRequest, "port", StringPool.BLANK);
user = ParamUtil.get(actionRequest, "user", StringPool.BLANK);
pass = ParamUtil.get(actionRequest, "password", StringPool.BLANK);
if(host.equals(StringPool.BLANK) || port.equals(StringPool.BLANK) ||
user.equals(StringPool.BLANK)) {
SessionErrors.add(actionRequest, "blank-fields");
} else {
PortletPreferences prefs = actionRequest.getPreferences();
prefs.setValue("host", host);
prefs.setValue("port", port);
prefs.setValue("user", user);
prefs.setValue("password", pass);
prefs.store();
System.out.println(String.format("Settings saved... [host: %s]",
prefs.getValue("host", "NULL")));
}
}
The last row, which pulls the host preference, shows me the correct string in the console. The controlpanel-page is then also filled with correct data. However, if I try to show the settings in my view.jsp nothing is returned! :(
Here's my doView(...)
:
@Override
public void doView(RenderRequest renderRequest,
RenderResponse renderResponse) throws IOException, PortletException {
ThemeDisplay themeDisplay =
(ThemeDisplay)renderRequest.getAttribute(WebKeys.THEME_DISPLAY);
String themeId = themeDisplay.getThemeId();
String jsp;
if(themeId.equalsIgnoreCase("controlpanel")) {
jsp = "/html/ekmsserviceoverview/controlpanel.jsp";
} else {
jsp = "/html/ekmsserviceoverview/view.jsp";
PortletPreferences prefs = renderRequest.getPreferences();
renderRequest.setAttribute("test", prefs.getValue("host", "NULL"));
}
this.viewTemplate = jsp;
super.doView(renderRequest, renderResponse);
}
In the controlpanel.jsp I was able to get the preferences via portletPreferences.getValue("host", "")
. In view.jsp neither that method nor getting the preference in the jsp by setting it with renderRequest.setAttribute(...)
worked.
What am I doing wrong? Is this the wrong way for saving those data?
Thanks in advance!
Upvotes: 0
Views: 1531
Reputation: 873
You should implement a Configuration Action to save preferences, or use Liferay's DefaultConfigurationAction.
However, changing the way you obtain preferences in your saveControlPanelSettings action may work:
PortletPreferences preferences;
String portletResource = ParamUtil.getString(request, "portletResource");
if (Validator.isNotNull(portletResource)) {
preferences = PortletPreferencesFactoryUtil.getPortletSetup(request, portletResource);
}
Upvotes: 0