1. 编写HelloWorldPortlet,HelloJSPPortlet类
1package com.educhina.portal;
2
3import javax.portlet.GenericPortlet;
4import java.io.IOException;
5import javax.portlet.PortletException;
6import javax.portlet.PortletRequestDispatcher;
7import javax.portlet.RenderRequest;
8import javax.portlet.RenderResponse;
9
10
11public class HelloJSPPortlet extends GenericPortlet {
12 public void doView(RenderRequest req, RenderResponse res)
13 throws IOException,PortletException
14 {
15 res.setContentType("text/html");
16 String jspName=getPortletConfig().getInitParameter("view-jsp");
17 PortletRequestDispatcher rd = getPortletContext().getRequestDispatcher(jspName);
18 rd.include(req,res);
19
20 }
21 public void doEdit(RenderRequest req, RenderResponse res)
22 throws IOException,PortletException
23 {
24 res.setContentType("text/html");
25 String jspName=getPortletConfig().getInitParameter("edit-jsp");
26 PortletRequestDispatcher rd=getPortletContext().getRequestDispatcher(jspName);
27 rd.include(req,res);
28 }
29
30}
31
HelloJSPPortlet: 调用jsp.
1package com.educhina.portal;
2
3import java.io.IOException;
4import javax.portlet.GenericPortlet;
5import javax.portlet.RenderRequest;
6import javax.portlet.RenderResponse;
7import javax.portlet.PortletException;
8public class HelloWorldPortlet extends GenericPortlet{
9 public void doView(RenderRequest req, RenderResponse res)
10 throws IOException,PortletException
11 {
12 res.setContentType("text/html");
13 res.getWriter().println("hello world");
14
15 }
16 public void doEdit(RenderRequest req, RenderResponse res)
17 throws IOException,PortletException
18 {
19 res.setContentType("text/html");
20 res.getWriter().println("hello world");
21 }
22
23}
HelloWorldPortlet:直接输出到portlet
2.为HelloJSPPortlet建立jsp页面.代码如下:
1<table cellpadding="8" cellspacing="0" width="100%">
2 <tr>
3 <td>
4 <font class="Portlet-font" style="font-size:x-small;">
5 this is a <b>Sample JSP Portlet</b> used in viewing in viewing model. use this a quick
6 way to include jsps.
7 </font>
8 </td>
9 </tr>
10</table>
11
注意:不能包含有<head><bady><html>的标签.只能包含原来是<body></body>内部的html内容,<head>,<body> <html>信息由Portal页面来提供. 只在在jsp页面里使用了
1<%@taglib uri="http://java.sun.com/portlet" prefix="portlet" %>
2<portlet:defineObjects/>
才可以直接操作portlet的一些变量,比如renderResponse, renderRequest, PortletConfig .
3.部署liferay portal
一>修改portlet.xml文件(C:\liferay-portal-tomcat-jdk5-4.2.1\webapps\ROOT\WEB-INF)中.
portlet定义portlet初始化信息
在<portlet-app>节点下增加:
1<portlet>
2 <portlet-name>HelloWorldPortlet</portlet-name>
3 <display-name>HelloWorldPortlet</display-name>
4 <portlet-class>com.educhina.portal.HelloWorldPortlet</portlet-class>
5 <expiration-cache>0</expiration-cache>
6 <supports>
7 <mime-type>text/html</mime-type>
8 </supports>
9 <supports>
10 <mime-type>text/html</mime-type>
11 <portlet-mode>edit</portlet-mode>
12 </supports>
13 <portlet-info>
14 <title>HelloWorldPortlet</title>
15 <short-title>HelloWorldPortlet</short-title>
16 <keywords>HelloWorldPortlet</keywords>
17 </portlet-info>
18 <security-role-ref>
19 <role-name>guest</role-name>
20 </security-role-ref>
21 <security-role-ref>
22 <role-name>power-user</role-name>
23 </security-role-ref>
24 <security-role-ref>
25 <role-name>user</role-name>
26 </security-role-ref>
27 </portlet>
28 <portlet>
29 <portlet-name>HelloJSPPortlet</portlet-name>
30 <display-name>HelloJSPPortlet</display-name>
31 <portlet-class>com.educhina.portal.HelloJSPPortlet</portlet-class>
32 <init-param>
33 <name>view-jsp</name>
34 <value>/portlet/testPortlet/view.jsp</value>
35 </init-param>
36 <init-param>
37 <name>edit-jsp</name>
38 <value>/portlet/testPortlet/edit.jsp</value>
39 </init-param>
40 <expiration-cache>0</expiration-cache>
41 <supports>
42 <mime-type>text/html</mime-type>
43 </supports>
44 <supports>
45 <mime-type>text/html</mime-type>
46 <portlet-mode>edit</portlet-mode>
47 </supports>
48 <portlet-info>
49 <title>HelloJSPPortlet</title>
50 <short-title>HelloJSPPortlet</short-title>
51 <keywords>HelloJSPPortlet</keywords>
52 </portlet-info>
53 <security-role-ref>
54 <role-name>guest</role-name>
55 </security-role-ref>
56 <security-role-ref>
57 <role-name>power-user</role-name>
58 </security-role-ref>
59 <security-role-ref>
60 <role-name>user</role-name>
61 </security-role-ref>
62 </portlet>
注意:
<init-param>
<name>edit-jsp</name>
<value>/portlet/testPortlet/edit.jsp</value>
</init-param>
告诉系jsp的位置,也就是HelloJSPPortlet类中
String jspName=getPortletConfig().getInitParameter("view-jsp");
PortletRequestDispatcher rd = getPortletContext().getRequestDispatcher(jspName);
rd.include(req,res);
所用到的.
所以编写好的edit.jsp放入(C:\liferay-portal-tomcat-jdk-mytest\webapps\ROOT\html\portlet\testPortlet)中.
二>修改liferay-portay.xml ------定义portlet模板,实例总数,是否允许重重定义等
<liferay-portay-app>节点下:
1 <portlet>
2 <portlet-name>HelloWorldPortlet</portlet-name>
3 <struts-path>HelloWorldPortlet</struts-path>
4 <use-default-template>true</use-default-template>
5 <instanceable>true</instanceable>
6 </portlet>
7 <portlet>
8 <portlet-name>HelloJSPPortlet</portlet-name>
9 <struts-path>HelloJSPPortlet</struts-path>
10 <use-default-template>true</use-default-template>
11 <instanceable>true</instanceable>
12 </portlet>
三>修改:liferay-display.xml---定义portlet所属的类别.liferay Portal定义了一个category.test类别.将HelloWorldPortlet和HelloJSPPortlet归属于category.test
<category name="category.test">
<portlet id="HelloWorldPortlet" />
<portlet id="HelloJSPPortlet" />
<portlet id="47" />
</category>
另外把两个类包拷贝到(C:\liferay-portal-tomcat-jdk-mytest\webapps\ROOT\WEB-INF\classes)目录下.
运行bin/startup.bat ok!