Open-Open

皇家撒拉哥萨
posts - 32, comments - 3, trackbacks - 0, articles - 1

Jetspeed-2 心得隨筆(2)-開發一個簡單的Portlet

Posted on 2006-07-17 17:03 开源爱好者 阅读(1468) 评论(0)  编辑  收藏 所属分类: Portal
這個portlet有一個text欄位輸入字串,submit後會在上方出現剛剛輸入的字串。

1. 延續之前的設定,將c:\tomcat\shared\lib\portlet-api-1.0.jar加入CLASSPATH

2. EchoPortlet.java :

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
package sample;
 
import java.io.IOException;
 
import javax.portlet.ActionRequest;
import javax.portlet.ActionResponse;
import javax.portlet.GenericPortlet;
import javax.portlet.PortletException;
import javax.portlet.PortletRequestDispatcher;
import javax.portlet.PortletSession;
import javax.portlet.RenderRequest;
import javax.portlet.RenderResponse;
 
publicclass EchoPortlet extends GenericPortlet{protectedvoid doView(RenderRequest renderRequest, RenderResponse renderResponse)throws PortletException, IOException {
        renderResponse.setContentType("text/html");
        
        String jspName = "/jsp/echo.jsp";
       
        PortletRequestDispatcher rd = getPortletContext().getRequestDispatcher(jspName);
        rd.include(renderRequest, renderResponse);
  }
 
  /* (non-Javadoc)
   * @see javax.portlet.GenericPortlet#processAction(javax.portlet.ActionRequest, javax.portlet.ActionResponse)
   */publicvoid processAction(ActionRequest actionRequest, ActionResponse actionResponse) throws PortletException, IOException {
    PortletSession session=actionRequest.getPortletSession();
    session.setAttribute("LAST_TYPED_STRING",actionRequest.getParameter("input")) ;
  }}
 


3. echo.jsp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
<%@ page language="java" session="true" import="javax.portlet.PortletSession"%>
<%@ taglib uri='/WEB-INF/portlet.tld' prefix='portlet'%>
<portlet:defineObjects/>
<portlet:actionURL var="action" />
<p></p>
<%
PortletSession  portletSession=renderRequest.getPortletSession();
String lastTypedString=(String)portletSession.getAttribute("LAST_TYPED_STRING");
if(lastTypedString!=null&&lastTypedString.length()>0){
%>
  The string you typed is:<b><%=lastTypedString%></b>
  <br/>
<%}%>
  <form action="<%=action%>" method="post">
    <input type="text" name="input" id="input" />
    <input type="submit" value="submit" />
  </form>
 
<p></p>


4. Portlet.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
<?xml version="1.0" encoding="UTF-8"?>
<portlet-app id="sample">
        <portlet id="EchoPortlet">
                <portlet-name>Echo Portlet</portlet-name>
                <portlet-class>sample.EchoPortlet</portlet-class>
 
                <expiration-cache>30</expiration-cache>
                <supports>
                        <mime-type>text/html</mime-type>
                        <portlet-mode>view</portlet-mode>
                </supports>
                <supported-locale>en-US</supported-locale>
                <portlet-info>
                        <title>EchoPortlet</title>
                        <short-title>EchoPortlet</short-title>
                        <keywords>EchoPortlet</keywords>
                </portlet-info>
                
        </portlet>
</portlet-app>


5. web.xml

1
2
3
4
5
<?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>Sample</display-name>
</web-app>


6. 準備sample.war檔,結構如下 ( portlet.tld 可從c:\tomcat\webapps\jetspeed\WEB-INF 拷貝 ):



7. 將sample.war拷到 C:\tomcat\webapps\jetspeed\deploy 下

8. 以admin這個帳號登入Jetspeed,點選左方的"Jetspeed Administrative Portlets"

9. 點選上方的"PALM"(Portlet Application Lifecycle Management),可以看到已經deploy的portlet application, 剛剛的sample應該在也在其中。可以從這裡undeploy / delete 不用的portlet application。

10. 接著點選上方的Portal Site Manager,會出現整個site目前的結構。點選"Root Folder"這個節點,右方會出現"Folder Details"的portlet。

11. 點選下方的"[Add Page]",然後在"Name"、"Title"、"Short Title"都輸入Sample,然後按"Save"。

12. 回到首頁,應該可以看到多了一個"Sample"的tab;在C:\tomcat\webapps\jetspeed\WEB-INF\pages下也多了一個Sample.psml的檔

13. 編輯Sample.psml如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<?xml version="1.0" encoding="UTF-8"?>
<page id="/Sample.psml" hidden="false" version="">
    <title>Sample</title>
    <short-title>Sample</short-title>
    <defaults layout-decorator="tigris" portlet-decorator="tigris"/>
    <fragment id="P-1082c720c8c-10000" type="layout" name="jetspeed-layouts::VelocityTwoColumns">
    
      <fragment id="28825252" type="portlet" name="sample::EchoPortlet">
        <property layout="TwoColumns" name="row" value="0" />
        <property layout="TwoColumns" name="column" value="0" />
      </fragment>
      
    </fragment>
</page>
 


14. 點選那個Sample 的tab,Portlet載入後應該就可以看到了



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


网站导航: