posts - 431,  comments - 344,  trackbacks - 0

需要portal-kernel.jar和portlet.jar两个包,放入lib下。
package com.rain.portal;

import Java.io.IOException;

import javax.portlet.GenericPortlet;
import javax.portlet.PortletException;
import javax.portlet.RenderRequest;
import javax.portlet.RenderResponse;

public class HelloWorldPortlet extends GenericPortlet {

 @Override
 protected void doEdit(RenderRequest arg0, RenderResponse arg1) throws PortletException, IOException {
  // TODO Auto-generated method stub
  arg1.setContentType("text/html");
  arg1.getWriter().println("Hello World,This is a Portlet!---doEdit");

 }

 @Override
 protected void doView(RenderRequest arg0, RenderResponse arg1) throws PortletException, IOException {
  // TODO Auto-generated method stub
  arg1.setContentType("text/html");
  arg1.getWriter().println("Hello World,This is a Portlet!---doView");

 }

}

package com.rain.portal;

import Java.io.IOException;

import javax.portlet.GenericPortlet;
import javax.portlet.PortletException;
import javax.portlet.PortletRequestDispatcher;
import javax.portlet.RenderRequest;
import javax.portlet.RenderResponse;

public class HelloJSPPortlet extends GenericPortlet {

 @Override
 protected void doEdit(RenderRequest arg0, RenderResponse arg1) throws PortletException, IOException {
  // TODO Auto-generated method stub
  arg1.setContentType("text/html");
  String jspName=getPortletConfig().getInitParameter("edit-jsp");
  PortletRequestDispatcher rd=getPortletContext().getRequestDispatcher(jspName);
  rd.include(arg0, arg1);
 }

 @Override
 protected void doView(RenderRequest arg0, RenderResponse arg1) throws PortletException, IOException {
  // TODO Auto-generated method stub
  arg1.setContentType("text/html");
  String jspName=getPortletConfig().getInitParameter("view-jsp");
  PortletRequestDispatcher rd=getPortletContext().getRequestDispatcher(jspName);
  rd.include(arg0, arg1);
 }

}


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>TestPortal</display-name>
  <listener>
    <listener-class>com.liferay.portal.kernel.servlet.PortletContextListener</listener-class>
  </listener>
  <servlet>
    <servlet-name>HelloWorldPortlet</servlet-name>
    <servlet-class>com.liferay.portal.kernel.servlet.PortletServlet</servlet-class>
    <init-param>
      <param-name>portlet-class</param-name>
      <param-value>com.rain.portal.HelloWorldPortlet</param-value>
    </init-param>
    <load-on-startup>0</load-on-startup>
  </servlet>
   <servlet>
    <servlet-name>HelloJSPPortlet</servlet-name>
    <servlet-class>com.liferay.portal.kernel.servlet.PortletServlet</servlet-class>
    <init-param>
      <param-name>portlet-class</param-name>
      <param-value>com.rain.portal.HelloJSPPortlet</param-value>
    </init-param>
    <load-on-startup>0</load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>HelloWorldPortlet</servlet-name>
    <url-pattern>/HelloWorldPortlet/*</url-pattern>
  </servlet-mapping>
  <servlet-mapping>
    <servlet-name>HelloJSPPortlet</servlet-name>
    <url-pattern>/HelloJSPPortlet/*</url-pattern>
  </servlet-mapping>
  <taglib>
    <taglib-uri>PortletTag</taglib-uri>
    <taglib-location>/WEB-INF/tld/liferay-portlet.tld</taglib-location>
  </taglib>
</web-app>

portlet.xml用来定义Portlet的诸如部署名称、初始化参数、支持模式、resource bundle等普通的初始化信息,包括:portlet-namedisplay-nameportlet-classinit-paramexpiration-cathesupportsportlet-infosecurity-role-ref等等。其正式的规范请参考:http://java.sun.com/xml/ns/Portlet/Portlet-app_1_0.xsd

portlet.xml
<?xml version="1.0"?>
<portlet-app xmlns="http://java.sun.com/xml/ns/portlet/portlet-app_1_0.xsd" version="1.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/portlet/portlet-app_1_0.xsdhttp://java.sun.com/xml/ns/portlet/portlet-app_1_0.xsd">
  <portlet>
    <portlet-name>HelloWorldPortlet</portlet-name>
    <display-name>HelloWorldPortlet</display-name>
    <portlet-class>com.rain.portal.HelloWorldPortlet</portlet-class>
    <expiration-cache>0</expiration-cache>
    <supports>
      <mime-type>text/html</mime-type>
    </supports>
    <supports>
      <mime-type>text/html</mime-type>
      <portlet-mode>edit</portlet-mode>
    </supports>
    <portlet-info>
      <title>HelloWorldPortlet</title>
      <short-title>HelloWorldPortlet</short-title>
      <keywords>HelloWorldPortlet</keywords>
    </portlet-info>
    <security-role-ref>
      <role-name>guest</role-name>
    </security-role-ref>
    <security-role-ref>
      <role-name>power-user</role-name>
    </security-role-ref>
    <security-role-ref>
      <role-name>user</role-name>
    </security-role-ref>
  </portlet>
  <portlet>
    <portlet-name>HelloJSPPortlet</portlet-name>
    <display-name>HelloJSPPortlet</display-name>
    <portlet-class>com.rain.portal.HelloJSPPortlet</portlet-class>
    <init-param>
      <name>view-jsp</name>
      <value>/view.jsp</value>
    </init-param>
    <init-param>
      <name>edit-jsp</name>
      <value>/edit.jsp</value>
    </init-param>
    <expiration-cache>0</expiration-cache>
    <supports>
      <mime-type>text/html</mime-type>
    </supports>
    <supports>
      <mime-type>text/html</mime-type>
      <portlet-mode>edit</portlet-mode>
    </supports>
    <portlet-info>
      <title>HelloJSPPortlet</title>
      <short-title>HelloJSPPortlet</short-title>
      <keywords>HelloJSPPortlet</keywords>
    </portlet-info>
    <security-role-ref>
      <role-name>guest</role-name>
    </security-role-ref>
    <security-role-ref>
      <role-name>power-user</role-name>
    </security-role-ref>
    <security-role-ref>
      <role-name>user</role-name>
    </security-role-ref>
  </portlet>
</portlet-app>

定义Portlet默认可用的用户组、默认模板、是否支持多个实例等,规范由http://www.liferay.com/dtd/liferay-Portlet-app_3_5_0.dtd

liferay-portlet.xml
<?xml version="1.0"?>
<!DOCTYPE liferay-portlet-app PUBLIC "-//Liferay//DTD Portlet Application 4.2.0//EN" "http://www.liferay.com/dtd/liferay-portlet-app_4_2_0.dtd">
<liferay-portlet-app>
  <portlet>
    <portlet-name>HelloWorldPortlet</portlet-name>
    <struts-path>HelloWorldPortlet</struts-path>
    <use-default-template>true</use-default-template>
    <instanceable>true</instanceable>
  </portlet>
  <portlet>
    <portlet-name>HelloJSPPortlet</portlet-name>
    <struts-path>HelloJSPPortlet</struts-path>
    <use-default-template>true</use-default-template>
    <instanceable>true</instanceable>
  </portlet>
  <role-mapper> 
    <role-name>administrator</role-name>
    <role-link>Administrator</role-link>
  </role-mapper>
  <role-mapper>
    <role-name>guest</role-name>
    <role-link>Guest</role-link>
  </role-mapper>
  <role-mapper>
    <role-name>power-user</role-name>
    <role-link>Power User</role-link>
  </role-mapper>
  <role-mapper>
    <role-name>user</role-name>
    <role-link>User</role-link>
  </role-mapper>
</liferay-portlet-app>

定义Portlet默认的所属类别。Liferay PortalPortlet实行按类别管理和划分用户权限。

liferay-display.xml
<?xml version="1.0"?>
<!DOCTYPE display PUBLIC "-//Liferay//DTD Display 4.0.0//EN" "http://www.liferay.com/dtd/liferay-display_4_0_0.dtd">
<display>
  <category name="category.test">
    <portlet id="HelloWorldPortlet"/>
    <portlet id="HelloJSPPortlet"/>
  </category>
</display>

view.jsp
<table cellpadding="8" cellspacing="0" width="100%">
  <tr>
    <td>
      <font class="Portlet-font" style="font-size:x-small;">
        This is a <b>Sample JSP Portlet</b> used in viewing model. Use this as a quick way to include JSPs.
      </font>
    </td>
  </tr>
</table>

edit.jsp
<table cellpadding="8" cellspacing="0" width="100%">
  <tr>
    <td>
      <font class="Portlet-font" style="font-size:x-small;">
        This is a <b>Sample JSP Portlet</b> used in editing model. Use this as a quick way to include JSPs.
      </font>
    </td>
  </tr>
</table>
posted on 2007-01-25 14:16 周锐 阅读(1784) 评论(2)  编辑  收藏 所属分类: Portal

只有注册用户登录后才能发表评论。


网站导航: