2

How do I add Portlet Preferences in Liferay 7, as we don't have portlet.xml anymore, what is the equivalent for the following in Liferay 7(OSGi Bundle).

<portlet-preferences>
   <preference>
      <name>isVeg</name>
      <value>VEG</value>
   </preference>
</portlet-preferences>  

2 Answers2

1

According to the Portlet Descriptor to OSGi service property Map:

@Component ( ...
   property="javax.portlet.preferences=<String>"
   ...
)

What this map leaves open is the syntax for the key-value pair. I'd start experimenting to duplicate the syntax of init-param, can't tell the exact syntax from the top of my head.

Edit: As stated in the comment, a quick look up in Liferay's source code reveals the following style, which is the only style used in Liferay itself:

"javax.portlet.preferences=classpath:/META-INF/portlet-preferences/default-portlet-preferences.xml"

Naturally, as this doesn't contain the values itself, you'll need to include them in your bundle as well (see the linked sample).

Olaf Kock
  • 46,930
  • 8
  • 59
  • 90
  • I tried with comma seperated(name,value) and ^ seperated. Nothing seems to work. :( – ashok maddy Dec 02 '17 at 02:43
  • Did you also try what I suggested, mirroring what `init-param` does? e.g. `javax.portlet.preferences.key=value`? Or, with a [quick look up in Liferay's source code, here's an example](https://github.com/liferay/com-liferay-announcements/blob/master/announcements-web/src/main/java/com/liferay/announcements/web/internal/portlet/AlertsPortlet.java#L47). This seems to be the only style used within Liferay's source. – Olaf Kock Dec 02 '17 at 08:40
  • I tried it both ways, giving 'preferences.key = value' and by having a separate xml file for portlet preference. Both didn't work. – ashok maddy Dec 04 '17 at 04:56
  • the separate xml works - see Liferay's source code that I've linked or just grep for occurrences of `javax.portlet.preferences` in Liferay's source code to see other occurrences. Repeat what those plugins do and you're set. Make sure that the resources end up in your bundle as well, under the name that you state, e.g. `/META-INF/portlet-preferences/default-portlet-preferences.xml` in the sample linked in my answer. – Olaf Kock Dec 04 '17 at 07:17
0

Let me try to give you an example point by point.

1. Create a Java interface.

@Meta.OCD(id = ColorPreferencePortletKeys.CONFIGURATION_ID)
public interface ColorConfiguration {
    @Meta.AD(deflt = "white", 
        name = "color", 
        optionLabels = { "%White", "%Red", "%Yellow" }, 
        optionValues = { "white", "red", "yellow" }, 
        required = false
    )
    public String color();
}

2. Create an action class

package com.example.portal.color.preferences.action;
@Component(
    configurationPid = ColorPreferencePortletKeys.CONFIGURATION_ID,
    configurationPolicy = ConfigurationPolicy.OPTIONAL, 
    immediate = true,
    property = "javax.portlet.name=" + ColorPreferencePortletKeys.PORTLET_ID,
    service = ConfigurationAction.class
)
public class ColorPreferencesAction extends DefaultConfigurationAction {
    private volatile ColorConfiguration colorConfiguration;
    @Override
    public void processAction(PortletConfig portletConfig, ActionRequest actionRequest, ActionResponse actionResponse) throws Exception {
        String color = ParamUtil.getString(actionRequest, "color");
        setPreference(actionRequest, "color", color);
        super.processAction(portletConfig, actionRequest, actionResponse);
    }
    @Activate
    @Modified
    protected void activate(Map<Object, Object> properties) {
        colorConfiguration = Configurable.createConfigurable(ColorConfiguration.class, properties);
    }
}

3. Implement your portlet class.

package com.example.portal.color.configuration.portlet
@Component(
    …  
)
public class ColorPreferencesPortlet extends MVCPortlet {
    private volatile ColorConfiguration colorConfiguration;
    @Override
    public void render(RenderRequest renderRequest, RenderResponse renderResponse) throws IOException, PortletException {
        renderRequest.setAttribute(ColorConfiguration.class.getName(), colorConfiguration);
        super.render(renderRequest, renderResponse);
    }
    @Activate
    @Modified
    protected void activate(Map<String, Object> properties) {
        colorConfiguration = ConfigurableUtil.createConfigurable(ColorConfiguration.class, properties);
    }
}

4. Now Implement your init.jsp

<%@page import="com.example.portal.config.ColorConfiguration"%>
<%@ page import="com.liferay.portal.kernel.util.GetterUtil" %>
<%@page import="com.liferay.portal.kernel.util.Validator"%>
<%@page import="com.liferay.portal.kernel.util.StringPool"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ taglib uri="http://java.sun.com/portlet" prefix="portlet" %>
<%@ taglib uri="http://liferay.com/tld/aui" prefix="aui" %>
<%@taglib uri="http://liferay.com/tld/portlet" prefix="liferay-portlet" %>
<%@taglib uri="http://liferay.com/tld/theme" prefix="liferay-theme" %>
<%@taglib uri="http://liferay.com/tld/ui" prefix="liferay-ui" %>
<liferay-theme:defineObjects />
<portlet:defineObjects />
<%
    ColorConfiguration colorConfiguration = (ColorConfiguration) renderRequest.getAttribute(ColorConfiguration.class.getName());
    String color = StringPool.BLANK;
    if (Validator.isNotNull(colorConfiguration)) {
        color = portletPreferences.getValue("color", colorConfiguration.color());
    }
%>

5. Create configuration.jsp

<%@ include file="/init.jsp" %>
<liferay-portlet:actionURL portletConfiguration="<%=true%>" var="configurationActionURL" />
<liferay-portlet:renderURL portletConfiguration="<%=true%>" var="configurationRenderURL" />
<aui:form action="<%=configurationActionURL%>" method="post" name="fm">
    <aui:input name="<%=Constants.CMD%>" type="hidden" value="<%=Constants.UPDATE%>" />
    <aui:input name="redirect" type="hidden" value="<%=configurationRenderURL%>" />
    <aui:fieldset>
        <aui:select label="Color" name="color" value="<%=color%>">
            <aui:option value="white">White</aui:option>
            <aui:option value="red">Red</aui:option>
            <aui:option value="yellow">Yellow</aui:option>
        </aui:select>
    </aui:fieldset>
    <aui:button-row>
        <aui:button type="submit"></aui:button>
    </aui:button-row>
</aui:form>

6. Add the below code in your view.jsp.

<%@ include file="/init.jsp"%>
<p>
    Favorite color: <span style="color: <%=color%>;"><%=color%></span>
</p>
Vijay Sasvadiya
  • 384
  • 1
  • 3
  • 7