2,Portal里的主角是Portlet,Portal页面上的一个个区域正是由Portlet组成的,如下图所示:(图片来源于Portlet in Action)
3,下面来开发一个Portlet项目,在Eclipse中新建一个web项目,目录结构如下:portlet.jar可以从下载的LifeRay的iferay-portal-tomcat-6.x.x.zip解压后的tomcat的lib/ext文件夹里找到.
3.1,HelloWorldPortlet的Java代码:
package com.test.portal; import java.io.IOException; import java.io.PrintWriter; import javax.portlet.GenericPortlet; import javax.portlet.PortletException; import javax.portlet.RenderRequest; import javax.portlet.RenderResponse; import javax.portlet.RenderMode; public class HelloWorldPortlet extends GenericPortlet { @RenderMode(name = "VIEW") public void sayHello(RenderRequest request, RenderResponse response) throws PortletException, IOException { PrintWriter out = response.getWriter(); out.println("My Hello World Portal"); } }@RenderMode(name = "VIEW")告诉Portal server sayHello方法是render 请求的view模式要调用的方法.
portlet.xml内容:
<?xml version="1.0" encoding="UTF-8"?> <portlet-app xmlns="http://java.sun.com/xml/ns/portlet/portlet-app_2_0.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="2.0" xsi:schemaLocation="http://java.sun.com/xml/ns/portlet/portlet-app_2_0.xsd http://java.sun.com/xml/ns/portlet/portlet-app_2_0.xsd"> <portlet> <portlet-name>HelloWorldPortlet</portlet-name> <display-name>Hello world</display-name> <portlet-class>com.test.portal.HelloWorldPortlet</portlet-class> <supports> <mime-type>text/html</mime-type> <portlet-mode>view</portlet-mode> </supports> <resource-bundle>content.Language-ext</resource-bundle> </portlet> </portlet-app>portlet.xml的内容都是符合Portlet标准规范的内容,<supports>的<portlet-mode>包括的可能值有view,edit和help(具体的意思在后续的文章中介绍).
liferay-portlet.xml: <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE liferay-portlet-app PUBLIC "-//Liferay//DTD Portlet Application 6.0.0//EN" "http://www.liferay.com/dtd/liferay-portlet-app_6_0_0.dtd"> <liferay-portlet-app> <portlet> <portlet-name>HelloWorldPortlet</portlet-name> <instanceable>true</instanceable> <remoteable>true</remoteable> </portlet> </liferay-portlet-app> liferay-display.xml: <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE display PUBLIC "-//Liferay//DTD Display 6.0.0//EN" "http://www.liferay.com/dtd/liferay-display_6_0_0.dtd"> <display> <category name="category.helloWorld"> <portlet id="HelloWorldPortlet" /> </category> </display> language-ext.properties: category.helloWorld=myPortlets javax.portlet.title=My Hello world portlet
language-ext.properties中定义的javax.portlet.title指定的值会被LifeRay用来作为Portlet的标题.category.helloWorld定义的值被liferay-display.xml的<category name用来指定protlet的类别.liferay-portlet.xml中的<portlet-name>HelloWorldPortlet</portlet-name>以及liferay-display.xml中的<portlet id="HelloWorldPortlet" />的值要和portlet.xml中的
<portlet-name>HelloWorldPortlet</portlet-name>保持一致.
将项目打包成war文件,拷贝到LifeRay运行环境的liferay-portal-6.0.3\deploy目录中,项目会自动发布到LifeRay Portal server.然后在Portal页面就可以把刚刚开发的portlet加入到页面中.Add-->More-->..Add
刚刚开发的portlet被加入到页面中:
完整的代码可以从http://download.csdn.net/detail/kkdelta/4081523下载