Sometime, we’d facing some simple job, such as storing the few portlet setting. If we using database approach, it will like having too much job. So, the following simple example i’ll demonstrate how can we develop something without fully rely on database, such as we can store some simple configuration into portlet itself.
Use case
For instance, your portlet is a feedback form, once the feedback is done. You’d like the form will automatic show the thank you page. And this thank you page will never maintain by you. The admin or liferay content user will create difference version of language page for each feedback form.
First, you will need a form object to hold the URL value
-
-
private static final long serialVersionUID = 1L;
-
private String url;
-
-
public WebForm () {}
-
-
return url;
-
}
-
-
this.url= url;
-
}
-
}
-
Getting data into PortletPreference
Next, In your controller default rendering method, get the portletpreference object form request object. The data store in portletpreference will be key and value. You can specified the default value from preference.getValue(“key”,”value”); For instance, the below example will get empty string if url key has no value.
-
-
@RenderMapping
-
RenderRequest request, RenderResponse response)
-
{
-
PortletPreferences preferences = request.getPreferences();
-
form.setUrl(url);
-
-
model.put("form", form);
-
return "form_setting_page";
-
}
-
Storing data into PortletPreference
-
-
@ActionMapping(params = "action=updateSetting")
-
public void doUpdate(@ModelAttribute("webForm") WebForm form, BindingResult result, ActionRequest request, ActionResponse response)
-
-
throws ReadOnlyException, ValidatorException, IOException
-
{
-
validator.validate(form, result);
-
if(!result.hasErrors()){
-
PortletPreferences preferences = request.getPreferences();
-
preferences.setValue("url", form.getUrl().trim());
-
preferences.store();
-
}else{
-
response.setRenderParameter("action", "");
-
}
-
}
-

Posted in 


