Liferay6.2添加可配置的portlet
详细参考地址:https://help.liferay.com/hc/zh-cn/articles/360018178611-Using-Configurable-Portlet-Preferences
获取配置参数
import javax.portlet.PortletPreferences;
PortletPreferences prefs = renderRequest.getPreferences();
// 文档媒体库文件夹
long folderId = Long.parseLong(prefs.getValue("folderId", "默认值"));
步骤一:创建配置文件页面的jsp页面,如:config.jsp
<%@ page contentType="text/html; charset=UTF-8"%>
<%@ include file="/init.jsp" %>
<%@ page import="com.liferay.portal.kernel.portlet.LiferayWindowState" %>
<%@ page import="com.liferay.portal.kernel.util.Constants" %>
<%@ page import="com.liferay.portal.kernel.util.HtmlUtil" %>
<%@ page import="com.liferay.portal.util.PortletKeys" %>
<%
%>
<liferay-portlet:actionURL portletConfiguration="true" var="configurationActionURL" />
<liferay-portlet:renderURL portletConfiguration="true" var="configurationRenderURL" />
<!-- 站点名称-->
<aui:form action="<%= configurationActionURL %>" method="post" name="fm1">
<aui:input name="<%= Constants.CMD %>" type="hidden" value="<%= Constants.UPDATE %>" />
<liferay-ui:panel-container extended="<%= true %>" id="documentLibrarySettingsPanelContainer" persistState="<%= true %>">
<!-- 选择文件夹 -->
<liferay-ui:panel-container extended="<%= true %>" id="documentLibrarySettingsPanelContainer" persistState="<%= true %>">
<liferay-ui:panel collapsible="<%= true %>" extended="<%= true %>" id="documentLibraryFoldersListingPanel" persistState="<%= true %>" title="folders-listing">
<!-- 文档媒体库 文件夹ID -->
<aui:input name='preferences--folderId--' type="hidden" value='<%=folderId%>'></aui:input>
<aui:fieldset>
<div class="control-group">
<aui:input label="内容标题" name="preferences--rootFolderName--" value="<%= rootFolderName %>" readonly='true'/>
<aui:button name="openFolderSelectorButton" value="select" />
<%
String taglibRemoveFolder = "Liferay.Util.removeFolderSelection('folderId', 'rootFolderName', '" + renderResponse.getNamespace() + "');";
%>
<aui:button disabled="<%= folderId <= 0 %>" name="removeFolderButton" onClick="<%= taglibRemoveFolder %>" value="remove" />
</div>
</aui:fieldset>
<!-- 网页内容 文件夹ID -->
<aui:input name='preferences--folderId3--' type="hidden" value='<%=folderId3%>'></aui:input>
<aui:fieldset>
<div class="control-group">
<aui:input label="内容标题" name="preferences--rootFolderName3--" value="<%= rootFolderName3 %>" readonly='true'/>
<aui:button name="openFolderSelectorButton3" value="select" />
<%
String taglibRemoveFolder3 = "Liferay.Util.removeFolderSelection('folderId3', 'rootFolderName3', '" + renderResponse.getNamespace() + "');";
%>
<aui:button disabled="<%= folderId3 <= 0 %>" name="removeFolderButton" onClick="<%= taglibRemoveFolder3 %>" value="remove" />
</div>
</aui:fieldset>
</liferay-ui:panel>
</liferay-ui:panel-container>
<!-- 显示类型 -->
<liferay-ui:panel collapsible="<%= true %>" extended="<%= true %>" id="journalArticlesDisplaySettings" persistState="<%= true %>" title="display-settings">
<aui:fieldset>
<!-- 每页的显示数量 -->
<aui:input label='display-per-page' name='preferences--pageDelta--' value='<%=pageDelta%>'></aui:input>
</aui:fieldset>
</liferay-ui:panel>
<liferay-ui:panel collapsible="<%= true %>" extended="<%= true %>" id="journalArticlesScrollSettings" persistState="<%= true %>" title="更多链接">
<aui:fieldset>
<aui:input label='更多' name='preferences--more--' value='<%=more%>'></aui:input>
</aui:fieldset>
</liferay-ui:panel>
</liferay-ui:panel-container>
<aui:button-row>
<aui:button type="submit" />
</aui:button-row>
</aui:form>
<%
String portletId = PortletKeys.DOCUMENT_LIBRARY;
String portletId3 = PortletKeys.JOURNAL;
%>
<liferay-portlet:renderURL portletName="<%=portletId%>" var="selectFolderURL" windowState="<%= LiferayWindowState.POP_UP.toString() %>">
<portlet:param name="struts_action" value='/document_library/select_folder' />
</liferay-portlet:renderURL>
<liferay-portlet:renderURL portletName="<%=portletId3%>" var="selectFolderURL3" windowState="<%= LiferayWindowState.POP_UP.toString() %>">
<portlet:param name="struts_action" value='/journal/select_folder' />
</liferay-portlet:renderURL>
<aui:script use="aui-base">
A.one('#<portlet:namespace />openFolderSelectorButton').on(
'click',
function(event) {
Liferay.Util.selectEntity(
{
dialog: {
constrain: true,
modal: true,
width: 1000
},
id: '_<%= HtmlUtil.escapeJS(portletId) %>_selectFolder',
title: '<liferay-ui:message arguments="folder" key="select-x" />',
uri: '<%= selectFolderURL.toString() %>'
},
function(event) {
var folderData = {
idString: 'folderId',
idValue: event.folderid,
nameString: 'rootFolderName',
nameValue: event.foldername
};
Liferay.Util.selectFolder(folderData, '<portlet:namespace />');
}
);
}
);
A.one('#<portlet:namespace />openFolderSelectorButton3').on(
'click',
function(event) {
Liferay.Util.selectEntity(
{
dialog: {
constrain: true,
modal: true,
width: 1000
},
id: '_<%= HtmlUtil.escapeJS(portletId3) %>_selectFolder',
title: '<liferay-ui:message arguments="folder" key="select-x" />',
uri: '<%= selectFolderURL3.toString() %>'
},
function(event) {
var folderData = {
idString: 'folderId3',
idValue: event.folderid,
nameString: 'rootFolderName3',
nameValue: event.foldername
};
Liferay.Util.selectFolder(folderData, '<portlet:namespace />');
}
);
}
);
</aui:script>
步骤二:创建资源加载文件,如:init.jsp
<%@ page contentType="text/html; charset=UTF-8" %>
<%@ taglib uri="http://java.sun.com/portlet_2_0" 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" %>
<%@ taglib uri="http://liferay.com/tld/util" prefix="liferay-util" %>
<%@ page import="javax.portlet.PortletPreferences" %>
<%@ page import="com.liferay.portal.kernel.util.StringPool" %>
<%@ page import="com.liferay.portal.kernel.util.GetterUtil" %>
<%@ page import="com.liferay.portal.kernel.util.ParamUtil" %>
<%@ page import="com.liferay.portal.kernel.util.HtmlUtil" %>
<%@ page import="java.util.List" %>
<%@ page import="com.liferay.portal.kernel.util.FastDateFormatFactoryUtil" %>
<%@ page import="java.text.Format" %>
<%@ page import="com.liferay.portlet.documentlibrary.model.DLFileEntry" %>
<%@ page import="java.net.URLEncoder" %>
<%@ page import="com.liferay.portlet.journal.model.JournalArticle" %>
<portlet:defineObjects />
<liferay-theme:defineObjects />
<%
PortletPreferences preference = renderRequest.getPreferences();
Format dateFormat = FastDateFormatFactoryUtil.getSimpleDateFormat("yyyy-MM-dd");
int pageDelta = GetterUtil.getInteger(preference.getValue("pageDelta", StringPool.BLANK));
String more = preference.getValue("more",StringPool.BLANK);
// 文件夹1
long folderId=GetterUtil.getLong(preference.getValue("folderId", StringPool.BLANK));
String rootFolderName = preference.getValue("rootFolderName", StringPool.BLANK);
String moreUrl = preference.getValue("moreUrl",StringPool.BLANK);
//文件夹3
long folderId3=GetterUtil.getLong(preference.getValue("folderId3", StringPool.BLANK));
String rootFolderName3 = preference.getValue("rootFolderName3", StringPool.BLANK);
String moreUrl3 = preference.getValue("moreUrl3",StringPool.BLANK);
String _portletTitle = HtmlUtil.escape(portletDisplay.getTitle());
%>
步骤三:修改liferay-portlet.xml配置文件,添加可配置项参数
<portlet>
<portlet-name>test-pc-portlet</portlet-name>
<icon>/icon.png</icon>
<configuration-action-class>com.liferay.portal.kernel.portlet.DefaultConfigurationAction</configuration-action-class>
<header-portlet-css>/css/main.css</header-portlet-css>
<footer-portlet-javascript>/js/main.js</footer-portlet-javascript>
</portlet>
步骤四:修改portlet.xml配置文件,添加配置页面地址
<portlet>
<portlet-name>test-pc-portlet</portlet-name>
<display-name>测试爬虫</display-name>
<portlet-class>com.test.TestPCPortlet</portlet-class>
<init-param>
<name>view-template</name>
<value>/view.jsp</value>
</init-param>
<init-param>
<name>config-template</name>
<value>/config.jsp</value>
</init-param>
<expiration-cache>0</expiration-cache>
<supports>
<mime-type>text/html</mime-type>
<portlet-mode>view</portlet-mode>
<portlet-mode>config</portlet-mode>
</supports>
<portlet-info>
<title>测试爬虫</title>
<short-title>测试爬虫</short-title>
<keywords>测试爬虫</keywords>
</portlet-info>
<security-role-ref>
<role-name>administrator</role-name>
</security-role-ref>
<security-role-ref>
<role-name>guest</role-name>
</security-role-ref>
<security-role-ref>
<role-name>power-user</role-name>
</security-role-ref>
<security-role-ref>
<role-name>user</role-name>
</security-role-ref>
</portlet>