有時候我們開發Portlet時會需要自訂configuration畫面,可能是用來讓使用者可以自行輸入一些參數之類的,例如在上傳檔案時就必須自行輸入FolderId,
在這裡紀錄一下使用方法。
首先開啟Portlet.xml檔,在需要自訂configuration的Portlet中<init-param>加上以下標籤
<init-param>
<name>config-template</name>
<value>/html/configuration.jsp</value>
</init-param>
然後開啟liferay-portlet.xml檔,在要加上動作的portlet<icon>之後加上;
<configuration-action-class>
com.liferay.portal.kernel.portlet.DefaultConfigurationAction
</configuration-action-class>
這樣就可以為該Portlet自訂新的configuration頁面,<value>中是jsp檔的位置。
接下來要新增一個configuration.jsp檔;
<%@page import="com.liferay.portal.kernel.util.Constants" %>
<portlet:defineObjects />
這裡是檔頭
<liferay-portlet:actionURL portletConfiguration="true" var="configurationURL" />
以下可放入init檔中。
<%
PortletPreferences preferences = renderRequest.getPreferences();
String portletResource = ParamUtil.getString(request, "portletResource");
if (Validator.isNotNull(portletResource)) {
preferences = PortletPreferencesFactoryUtil.getPortletSetup(request, portletResource);
然後這裡先設定範例變數configID
String configID= GetterUtil.getInteger(preferences.getValue("aa", null));
這樣就開啟Configuration時便可以看見之前輸入的參數,此參數是存放在liferay資料庫內。
%>
*以上基本上可以放入init.jsp,這樣在jsp頁面載入時,只要引入init.jsp即可在原本的頁面取得設定資料
<aui:form action="<%=configurationURL%>" method="post">
//下面這行是用來控制寫入資料的
<aui:input name="<%= Constants.CMD %>" type="hidden" value="<%= Constants.UPDATE %>" />
<!-- 自訂參數輸入框
label是顯示標籤,name 是你要寫入Preferences的變數,請使用preferences--變數名稱--
這裡是用aa作為變數
-->
<aui:input label="" name="preferences--aa--" value="<%=configID %>" />
<aui:button type="submit" />
</aui:form>
到這裡基本上就完成了,以下以實例說明;
---------------------------------------------------------------------------------------
如果我今天需要上傳檔案用的folderId,init檔案內會這樣設定;
String docFolderId="";
if(preferences.getValue("docFolderId", null)!=null){
docFolderId=preferences.getValue("docFolderId", null);
}
以上先宣告一個字串folderId,用docfolderId這個name去抓。
接著是configuration.jsp,裡面加上;
<%
docFolderId=GetterUtil.getString(preferences.getValue("docFolderId", null));
%>
以下兩行是用來控制寫入
<aui:form action="<%=configurationURL%>" method="post">
<aui:input name="<%= Constants.CMD %>" type="hidden" value="<%= Constants.UPDATE %>" />
下面這一行便是顯示的參數輸入欄位;
<aui:input label="docFolderId" name="preferences--docFolderId--" value="<%=docFolderId %>" />
寫完後基本上長得像這樣;
接著只要輸入參數並儲存,然後在想要抓這個參數的頁面設定一個hidden的輸入欄位;
<!-- 下面這行是用來抓folderId的 -->
<aui:input type="hidden" name="videoFolderId" value="<%=videoFolderId %>" />
這樣後端就可以抓到參數並拿來用囉!
