Liferay 探究竟(3) — Struts in Liferay

Yi-Chan Kao
Aug 9, 2017 · 6 min read

( 本系列文所用來說明的 Liferay 版本為 6.1.0 CE GA1)

在 Liferay 我們會看到很多不同的 portlet 出現在畫面上,但是 portlet 是怎麼被 render 出來的呢?例如下圖這個 Document and Media portlet:

要能夠看到這個內容的呈現,Liferay 內部是透過 struts 框架來處理,但又自己實作了一些東西。 (編按:在 Liferay 的 source code 裡面常常可以看到他們不直接引用別人的 library,而是會根據 library 的 interface 自己實作一份,然後在系統內部使用自己的實作)

但在開始說明之前,先來補充一些基本知識:

所謂的 portlet 雖然跟 servlet 有些類似,但其實是不同的東西,這邊也附上portlet 的規格給大家參考。而其中第八章有談到 portlet 有三種模式:View、Edit、Help,我們這邊先只談論 View 模式。

準備開始囉!

這邊假設大家都已經架好了一個 Liferay 的 instance 並且成功地運作起來了,也擁有可以觀看 Liferay portal 檔案的權限,手邊也有 source code 可以看。

要怎麼樣產生像上圖那樣的畫面呢?首先我們要來看一個 portlet 是怎麼被 render 的。

在 render_portlet.jsp 中我們可以看到以下這段

render_portlet.jsp

這裡表明了在 render portlet 的過程中會去找 portlet init param 中的 “view-action” 設定值,以取得所需的 ActionMapping 設定。

“view-action” 設定值則被寫在 portlet-custom.xml 中,以 Document and Media portlet 為例,會找到這樣的內容

portlet-custom.xml

由此可以得知,這個設定值是 /document_library/view

那要怎麼取得 ActionMapping 呢,系統會藉由剛剛找到的設定值去 struts-config.xml 查找到設定,由上例我們可以拿到這樣的定義:

struts-config.xml

由此可知,這組動作會呼叫到 com.liferay.portlet.documentlibrary.action.ViewAction,我們就去看他的 source code 吧!

com.liferay.portlet.documentlibrary.action.ViewAction

我們可以看到最後他會 forward 到 portlet.document_library.view。這東西似乎有點眼熟,原來在上一動的定義裡面有看到這個 forward 的定義:

struts-config.xml

他對應的路徑是 portlet.document_library.view。那這路徑到底是在哪裡呢?我們可以去 tiles-def.xml 找到這個路徑的定義

tiles-def.xml

由此可知,他其實是導向到 /portlet/document_library/view.jsp 這個檔案去了,我們只要看這個檔案就可以找到為何會是這樣產生 html。

皆大歡喜!我們終於知道 portlet 是怎麼長出來的了,但過程中總覺得怪怪的!

為什麼在看 com.liferay.portlet.documentlibrary.action.ViewAction 的時候,我們知道要去看 render 這個 method 呢?

這時候一樣要談到 portlet 的規格了,在第七章有講到 portlet 的 url 有分為三種:ActionURL、RenderURL、ResourceURL。而對應到 Liferay 的 PortletAction (上例的ViewAction 即實作此 class ) 則是如下的情況:

使用 ActionURL 呼叫 PortletAction 時會對應到 processAction 這個 method

使用 RenderURL 呼叫 PortletAction 時會對應到 render 這個 method

使用 ResourceURL 呼叫 PortletAction 時會對應到 serveResource 這個 method

一樣拿 Document and Media portlet 來看,編輯檔案時會 render /portlet/document_library/edit_file_entry.jsp,在完成編輯的時候,form 會打到這樣的一個 URL

/portlet/document_library/edit_file_entry.jsp

由圖可知,他是一個 actionURL,會打到 struts_action 為 /document_library/edit_file_entry 的 action,鏡頭轉到 struts-config.xml,我們可以看到如下

struts-config.xml

我們可以得知,他會去找 com.liferay.portlet.documentlibrary.action.EditFileEntryAction 而且會去呼叫 processAction,我們就可以來看 source code 在做些什麼了

com.liferay.portlet.documentlibrary.action.EditFileEntryAction

Reference

Overriding and Adding Struts Actions

Portlet spec. v2.0 (JSR-286)

— —

Liferay 探究竟系列文

Liferay 探究竟(1) — 權限系統簡介

Liferay 探究竟(2) — 自訂欄位

Liferay 探究竟(3) — Struts in Liferay

Liferay 探究竟(4) — 主題開發

Liferay 探究竟(5) — 學習資源整理

Yi-Chan Kao

Written by

Full-stack(?) Web Application Developer in Taipei, Taiwan.

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade