談了這么多,還沒說怎么自己往Liferay中創(chuàng)建和加入一個portlet,Liferay中定義了幾種類型的portlet,如JSPPortlet,StrutsPortlet,和VelocityPortlet。先以JSPPortlet為例說明吧。
1. 定義新的JSP Portlet
a) 首先到 ...\portlet\ext中去加入自己要創(chuàng)建的portlet文件夾,例如myappletportlet,在文件夾中創(chuàng)建一個view.jsp,其中的內(nèi)容可最簡單為:
Hello JSPPortlet world!
最好的起始學習方法應(yīng)該是照葫蘆畫瓢吧,找一個已有的portlet,看它的結(jié)構(gòu)和代碼。
b) 到 ...\WEB-INF\portlet-ext.xml 中加入
<portlet>
<portlet-name>EXT_2</portlet-name>
<!-- portlet 的最關(guān)鍵ID -->
<display-name>My AppletPortlet</display-name>
<portlet-class>com.liferay.portlet.JSPPortlet</portlet-class>
<!-- portlet 所屬的類 -->
<init-param>
<name>view-jsp</name>
<value>/portlet/ext/myappletportlet/view.jsp</value>
<!-- MVC中會直接傳遞到view.jsp -->
</init-param>
<expiration-cache>300</expiration-cache>
<supports>
<mime-type>text/html</mime-type>
</supports>
<portlet-info>
<title>PENG Portlet</title>
<!-- Portlet上顯示的名字 -->
</portlet-info>
<security-role-ref>
<role-name>Power User</role-name>
</security-role-ref>
<security-role-ref>
<role-name>User</role-name>
</security-role-ref>
</portlet>
c) 到 ...\WEB-INF\liferay-portlet-ext.xml 中加入
<portlet>
<portlet-name>EXT_2</portlet-name>
</portlet>
d) 到...\WEB-INF\liferay-display.xml 中加入以下代碼,以將EXT_2加入sample分類,到時候在Add Content時可以在這個sample組找到,否則在undefined中找。
<category name="category.sample">
<portlet id="47"/>
......
<portlet id="EXT_2"/>
</category>
e) 測試。如果上述的修改是直接在tomcat目錄中進行的話,重新啟動tomcat。如果是在前面提到的自己創(chuàng)建的EXT環(huán)境下進行的話,先用ant一下,再啟動tomcat。然后http://localhost:8080/,用test@liferay.com/test進去,選擇Add Content,應(yīng)該可以在sample目錄下找到前面創(chuàng)建的那個portlet了:PENG Portlet。
2. 定義新的StrutsPortlet。過程要略微復(fù)雜一些,誰叫我們要用傳說中的MVC呢。先列出幾個關(guān)鍵的配置文件:
portlet-ext.xml: 定義portlet(JSR-168 attributes)
liferay-portlet-ext.xml: 注冊portlet(Liferay attributes)
struts-config.xml: 定義 page-flow (action mapping)
tiles-defs.xml: 定義 the page layout
更詳細的信息可從Liferay的官方網(wǎng)站上獲得。
a) 在ext中增加一個叫做Tiles的portlet文件夾,在其中創(chuàng)建一個view.jsp。內(nèi)容可以簡單為:
Hello StrutsPortlet world!
b) Portlet-ext.xml 中加入
<portlet>
<portlet-name>EXT_3</portlet-name>
<display-name>Tiles</display-name>
<portlet-class>
com.liferay.portlet.StrutsPortlet
<!—實現(xiàn)JSR-168規(guī)范的類 -->
</portlet-class>
<init-param>
<name>view-action</name>
<value>/ext/tiles/view</value>
<!-- portal會到struts-config.xml中去尋找/ext/tiles/view -->
</init-param>
<expiration-cache>0</expiration-cache>
<supports>
<mime-type>text/html</mime-type>
</supports>
<resource-bundle>
com.liferay.portlet.StrutsResourceBundle
</resource-bundle>
<security-role-ref>
<role-name>user</role-name>
</security-role-ref>
</portlet>
c) liferay-portlet-ext.xml中加入
<portlet>
<portlet-name>EXT_3</portlet-name>
<struts-path>ext/tiles</struts-path>
<!-- 告訴portal所有帶有ext/tiles/*路徑的請求可以認為是這個portlet的范圍 -->
<use-default-template>false</use-default-template>
<!-- 因為將采用別的template,所以設(shè)置為false。將要使用的template在tiles-defs.xml中定義。-->
</portlet>
d) struts-config-ext.xml中加入
<action path="/ext/tiles/view"
forward="portlet.ext.tiles.view" />
<!-- Struts 將會在 tiles-defs.xml 中尋找 portlet.ext.tiles.view -->
e) 在tiles-defs-ext.xml 中加入
<definition name="portlet.ext.tiles.view" extends="portlet">
<!-- 定義了哪個 template 將被使用,這里是portlet template。這個template 定義了portlet的borders和buttons(例如minimize, maximize, close 等等)。 -->
<put name="portlet_content" value="/portlet/ext/tiles/view.jsp" />
<!-- portlet_content 是一個liferay的變量,portal可以使用這個變量來決定在portlet中會呈現(xiàn)什么內(nèi)容。這里portlet的內(nèi)容就是view.jsp。 -->
</definition>
f) 測試。
Trackback: http://tb.blog.csdn.net/TrackBack.aspx?PostId=2177436