在 Liferay 7 中添加 Portlet 首选项

Adding Portlet Preferences in Liferay 7

我如何在 Liferay 7 中添加 Portlet 首选项,因为我们不再有 portlet.xml,Liferay 7(OSGi Bundle) 中以下内容的等价物是什么。

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

根据 Portlet Descriptor to OSGi service property Map

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

此映射未解决的是键值对的语法。我会开始尝试复制 init-param 的语法,无法从我的脑海中分辨出确切的语法。

编辑:正如评论中所述,quick look up in Liferay's source code 揭示了以下样式,这是 Liferay 本身使用的唯一样式:

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

当然,由于它本身不包含值,因此您还需要 include them in your bundle(请参阅链接示例)。

让我逐点举例说明。

1.创建一个Java接口。

@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。创建动作 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。实施您的 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.现在实施您的 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.创建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.在您的 view.jsp.

中添加以下代码
<%@ include file="/init.jsp"%>
<p>
    Favorite color: <span style="color: <%=color%>;"><%=color%></span>
</p>