JavaWorld@TW the best professional Java site in Taiwan
      註冊 | 登入 | 全文檢索 | 排行榜  

» JavaWorld@TW » Java Tools  

按列印兼容模式列印這個話題 列印話題    把這個話題寄給朋友 寄給朋友   
reply to topicthreaded modego to previous topicgo to next topic
己加入精華區
by jini at 2005-12-18 05:50
本主題所含的標籤
無標籤
作者 Jetspeed-2 心得隨筆(2)-開發一個簡單的Portlet [精華]
wayyoung





發文: 21
積分: 1
於 2005-12-15 14:35 user profilesend a private message to userreply to postreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
這個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;
 
public class EchoPortlet extends GenericPortlet{
  protected void 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)
   */
  public void 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載入後應該就可以看到了


vote up 0 vote down
wayyoung edited on 2005-12-15 14:55
reply to postreply to post

» JavaWorld@TW »  Java Tools

reply to topicthreaded modego to previous topicgo to next topic
  已讀文章
  新的文章
  被刪除的文章
Jump to the top of page

JavaWorld@TW


Powered by Powerful JuteForum® Version Jute 1.5.8
Copyright© 2002-2003 Rainman Zhu,Zua,Netboy,Scott. All Rights Reserved.