本指南介绍了开发一个非常简单的 portlet。 Portlet 开发者可以按照下面步骤完成。
在 simplest/WEB-INF/classes 文件夹下新建一个 Simplest.java 文件:
public class Simplest extends javax.portlet.GenericPortlet { public void doView(javax.portlet.RenderRequest request, javax.portlet.RenderResponse response) throws javax.portlet.PortletException, java.io.IOException { response.setContentType("text/html"); response.getWriter().println("A very simple portlet."); } }
可以通过下面的命令进行编译:
javac -cp ~/.maven/repository/org.apache.portals.jetspeed-2/jars/portlet-api-1.0.jar Simplest.java
在 simplest/WEB-INF 文件夹下新建 portlet.xml 文件。
<?xml version="1.0" encoding="UTF-8"?> <portlet-app id="simplest" version="1.0"> <portlet id="Simplest"> <portlet-name>Simplest</portlet-name> <display-name>Simple Display Name</display-name> <portlet-class>Simplest</portlet-class> <supports> <mime-type>text/html</mime-type> <portlet-mode>VIEW</portlet-mode> </supports> <supported-locale>en</supported-locale> <portlet-info> <title>Simple Title</title> <short-title>The world's simplest portlet</short-title> </portlet-info> </portlet> </portlet-app>
在 WEB-INF 文件夹下建立 web.xml 文件。
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd"> <web-app> <display-name>Simplest</display-name> <description>The world's simplest portlet</description> </web-app>
在 Jetspeed Portal 的 src/webapp/WEB-INF/pages 目录下建立一个 simplest.psml 文件。 这个 portlet 应用的 id 和名字合起来作为 portlet fragment 的标识。重建你的 portal。
<?xml version="1.0" encoding="UTF-8"?> <page> <defaults skin="orange" layout-decorator="tigris" portlet-decorator="tigris" /> <title>The simplest portlet in the world</title> <metadata name="title" xml:lang="fr">La plus simple portlet du monde</metadata> <fragment id="simplest" type="layout" name="jetspeed-layouts::VelocityTwoColumns"> <fragment id="simplest-1" type="portlet" name="simplest::Simplest"> <property layout="TwoColumns" name="row" value="0" /> <property layout="TwoColumns" name="column" value="0" /> </fragment> </fragment> <security-constraints> <security-constraints-ref>public-view</security-constraints-ref> </security-constraints> </page>