X

Subscribe to BloggerHow updates..

Recommend us on Google+

Get updates in your mail

New posts will get delivered to your mail box directly.Its free and we never spam you. Enter your mail here and click on subscribe. Check your mail box for a confirmation mail.

Get updates in your RSS reader

Subscribe using RSS and read updates using your favorite RSS reader..
RSS

Like us on Facebook

Get updates directly on your Facebook wall.

Follow us on Twitter

Get updates as tweets..

Circle me on Google plus

Add me to your circle and get new post updates in Google plus stream.

To set preference for a portlet we need to follow below steps

1.Add below entries in liferay-portlet.xml file

<configuration-action-class>com.portlet.action.configuratoionImpl</configuration-action-class>

Before <instanceable>true</instanceable>

2.Create a custom action class which implements ConfigurationAction interface.

A sample Class
public class configuratoionImpl implements ConfigurationAction {

       @Override
       public void processAction(PortletConfig portletConfig, ActionRequest actionRequest,
                     ActionResponse actionResponse) throws Exception {

                           String cmd = ParamUtil.getString(actionRequest, Constants.CMD);
                           ThemeDisplay themeDisplay=(ThemeDisplay) actionRequest.getAttribute(WebKeys.THEME_DISPLAY);
                           if (!cmd.equals(Constants.UPDATE)) {
                                  return;
                           }
//Fetching String values from configuration.jsp page
String noofrecords = ParamUtil.getString(actionRequest, "recordsperpage");
String uname = ParamUtil.getString(actionRequest, "uname");
                          
       String portletResource = ParamUtil.getString(
                                  actionRequest, "portletResource");

PortletPreferences preferences =
                                  PortletPreferencesFactoryUtil.getPortletSetup(
                                         actionRequest, portletResource);
//set all values in preferences and store                           
preferences.setValue("noofrecords", noofrecords);
preferences.setValue("uname", uname);

preferences.store();
                          
SessionMessages.add(actionRequest, portletConfig.getPortletName() + ".doConfigure");
             
       }

       @Override
       public String render(PortletConfig arg0, RenderRequest renderRequest,
                     RenderResponse renderResponse) throws Exception {
              //redirect to configuration jsp
              return "/jsp/configuration.jsp";
       }

}

3.Create a configuration.jsp under the jsp folder

<%
                String redirect = ParamUtil.getString(request, "redirect");

                PortletPreferences preferences = renderRequest.getPreferences();
                String portletResource = ParamUtil.getString(request, "portletResource");
                if (Validator.isNotNull(portletResource)) {
                                preferences = PortletPreferencesFactoryUtil.getPortletSetup(request, portletResource);
                }
                               
%>

<liferay-portlet:actionURL portletConfiguration="true" var="configurationURL" />

<form action="<%= configurationURL.toString() %>" method="post" name="<portlet:namespace />fm">
                <input name="<portlet:namespace />redirect" type="hidden" value="<%= HtmlUtil.escape(redirect) %>" />
                <input name="<portlet:namespace /><%= Constants.CMD %>" type="hidden" value="<%= Constants.UPDATE %>" />
               
                <aui:fieldset label="Configurations">
                <aui:layout>
                <aui:column  first="true">
                                               
                                <table >
                                               
                                                <tr>
                                                                <td><liferay-ui:message key="name" />&nbsp;&nbsp;</td>
                                                                <td  class="lpad15">
                                                                                <aui:input name="name" type="text"  label="" value='<%=  preferences.getValue("uname", "") == null ? "10" : preferences.getValue("uname", "") %>' ></aui:input>
                                                                </td>
                                                </tr>
                                               
                                                <tr>
                                                                <td><liferay-ui:message key="RecordsperPage" />&nbsp;&nbsp;</td>
                                                                <td  class="lpad15">
                                                                                <aui:input name="recordsperpage" type="text"  label="" value='<%=  preferences.getValue("noofRecords", "") == null ? "10" : preferences.getValue("noofRecords", "") %>' ></aui:input>
                                                                </td>
                                                </tr>
                                                <tr>
                                                                <td>
                                                                                <aui:button type="submit" value="save"></aui:button>
                                                                </td>
                                                </tr>
                                </table>
               
               
                </aui:column>
                </aui:layout>
                </aui:fieldset>
</form>


Share article
Older Post

0 comments:

Post a Comment

To Top