使用RAD7.5开发Portlet的例子

发布时间:2011-02-16 17:27:46

所有的portlet类理论上讲都应该implements Portlet这个interface,但是实际开发中一般是继承GenericPortlet这个类,这个类是Portlet这个接口的一般实现,而且这个类在jsr286规范中实现了Portlet, PortletConfig, EventPortlet, ResourceServingPortlet这几个接口。

Portlet 开发一定要明白portlet运行机制, portletjsp的请求分为两种:renderRequestactionRequest ,其中 actionRequest 触发的是portlet 类中的processAction() method,在经过这个方法处理之后 portlet容器 会触发这个页面上所有portletRender()method,而 render()这个method 会去调用doDispatch()method分发请求到 doview(),从而把要显示的页面显示出来;如果一个request renderRequest的时候 这时候portlet容器会掉用 这个页面上的所有的portletRender()method来刷新显示所有portlet

,其中doDispatch这个method是用来转发请求的。

好了下面来看具体的开发细节:

1.打开 rad7.5,建立一个portlet项目名字叫 newEventxx,选择api类型为 jsr286portlet类型为基本portlet,其他默认,点击下一步,。。完成。

2.newEventxx项目上点右键==〉新建==portlet ,添加第二个portlet到项目中,

这时候系统给我们建立了四个文件:

分别叫NewEventxxPortlet.Java.。。。。等等

这四个文件中 NewEventxxPortlet.Java NewEventxx2Portlet.Javaf分别是第一个portlet和第二个portlet的请求处理类,另外两个java文件为该portlet对应的传递数据的bean

3. NewEventxxPortletSessionBean作为信息传递类,其中信息传递类需要 实现Serializable这个接口,故在第三步把这个bean 改写成为实现Serializable这个接口

以上为 这个bean的源代码

4.配置 portletXml文件

PortletXml的格式现在应该为:

<portlet-app>

NewEventxxPortlet定义信息

NewEventxx2Portlet定义信息

http://newEvent/

portlet-app>

添加Event定义 ,event发布,event接受处理后的xml文件应该为:

<portlet-app>

NewEventxxPortlet定义信息)

name

NewEventxx2Portlet定义信息)

name

http://newEvent/

name

portlet-app>

5.改写newEventxxPortletJava添加信息发布操作和改写newEventxx2Portlet.java 添加事件处理方法。

package com.ibm.neweventxx;

import java.io.*;

import java.util.*;

import javax.portlet.*;

/**

* A sample portlet based on GenericPortlet

*/

public class NewEventxxPortlet extends GenericPortlet {

public static final String JSP_FOLDER = "/_newEventxx/jsp/"; // JSP folder name

public static final String VIEW_JSP = "NewEventxxPortletView"; // JSP file name to be rendered on the view mode

public static final String SESSION_BEAN = "NewEventxxPortletSessionBean"; // Bean name for the portlet session

public static final String FORM_SUBMIT = "NewEventxxPortletFormSubmit"; // Action name for submit form

public static final String FORM_TEXT = "NewEventxxPortletFormText"; // Parameter name for the text input

/**

* @see javax.portlet.Portlet#init()

*/

public void init() throws PortletException{

super.init();

}

/**

* Serve up the view mode.

*

* @see javax.portlet.GenericPortlet#doView(javax.portlet.RenderRequest, javax.portlet.RenderResponse)

*/

public void doView(RenderRequest request, RenderResponse response) throws PortletException, IOException {

// Set the MIME type for the render response

response.setContentType(request.getResponseContentType());

// Check if portlet session exists

NewEventxxPortletSessionBean sessionBean = getSessionBean(request);

if( sessionBean==null ) {

response.getWriter().println("NO PORTLET SESSION YET");

return;

}

// Invoke the JSP to render

PortletRequestDispatcher rd = getPortletContext().getRequestDispatcher(getJspFilePath(request, VIEW_JSP));

rd.include(request,response);

}

/**

* Process an action request.

*

* @see javax.portlet.Portlet#processAction(javax.portlet.ActionRequest, javax.portlet.ActionResponse)

*/

public void processAction(ActionRequest request, ActionResponse response) throws PortletException, java.io.IOException {

if( request.getParameter(FORM_SUBMIT) != null ) {

// Set form text in the session bean

NewEventxxPortletSessionBean sessionBean = getSessionBean(request);

if( sessionBean != null )

sessionBean.setFormText(request.getParameter(FORM_TEXT));

response.setEvent("name",sessionBean );

}

}

/**

* Get SessionBean.

*

* @param request PortletRequest

* @return NewEventxxPortletSessionBean

*/

private static NewEventxxPortletSessionBean getSessionBean(PortletRequest request) {

PortletSession session = request.getPortletSession();

if( session == null )

return null;

NewEventxxPortletSessionBean sessionBean = (NewEventxxPortletSessionBean)session.getAttribute(SESSION_BEAN);

if( sessionBean == null ) {

sessionBean = new NewEventxxPortletSessionBean();

session.setAttribute(SESSION_BEAN,sessionBean);

}

return sessionBean;

}

/**

* Returns JSP file path.

*

* @param request Render request

* @param jspFile JSP file name

* @return JSP file path

*/

private static String getJspFilePath(RenderRequest request, String jspFile) {

String markup = request.getProperty("wps.markup");

if( markup == null )

markup = getMarkup(request.getResponseContentType());

return JSP_FOLDER + markup + "/" + jspFile + "." + getJspExtension(markup);

}

/**

* Convert MIME type to markup name.

*

* @param contentType MIME type

* @return Markup name

*/

private static String getMarkup(String contentType) {

if( "text/vnd.wap.wml".equals(contentType) )

return "wml";

else

return "html";

}

/**

* Returns the file extension for the JSP file

*

* @param markupName Markup name

* @return JSP extension

*/

private static String getJspExtension(String markupName) {

return "jsp";

}

}

======================================================================package com.ibm.neweventxx;

import java.io.*;

import java.util.*;

import javax.portlet.*;

/**

* A sample portlet based on GenericPortlet

*/

public class NewEventxx2Portlet extends GenericPortlet {

public static final String JSP_FOLDER = "/_newEventxx2/jsp/"; // JSP folder name

public static final String VIEW_JSP = "NewEventxx2PortletView"; // JSP file name to be rendered on the view mode

public static final String SESSION_BEAN = "NewEventxx2PortletSessionBean"; // Bean name for the portlet session

public static final String FORM_SUBMIT = "NewEventxx2PortletFormSubmit"; // Action name for submit form

public static final String FORM_TEXT = "NewEventxx2PortletFormText"; // Parameter name for the text input

/**

* @see javax.portlet.Portlet#init()

*/

public void init() throws PortletException{

super.init();

}

/**

* Serve up the view mode.

*

* @see javax.portlet.GenericPortlet#doView(javax.portlet.RenderRequest, javax.portlet.RenderResponse)

*/

public void doView(RenderRequest request, RenderResponse response) throws PortletException, IOException {

// Set the MIME type for the render response

response.setContentType(request.getResponseContentType());

// Check if portlet session exists

NewEventxx2PortletSessionBean sessionBean = getSessionBean(request);

if( sessionBean==null ) {

response.getWriter().println("NO PORTLET SESSION YET");

return;

}

// Invoke the JSP to render

PortletRequestDispatcher rd = getPortletContext().getRequestDispatcher(getJspFilePath(request, VIEW_JSP));

rd.include(request,response);

}

/**

* Process an action request.

*

* @see javax.portlet.Portlet#processAction(javax.portlet.ActionRequest, javax.portlet.ActionResponse)

*/

public void processAction(ActionRequest request, ActionResponse response) throws PortletException, java.io.IOException {

if( request.getParameter(FORM_SUBMIT) != null ) {

// Set form text in the session bean

NewEventxx2PortletSessionBean sessionBean = getSessionBean(request);

if( sessionBean != null )

sessionBean.setFormText(request.getParameter(FORM_TEXT));

}

}

/**

* Get SessionBean.

*

* @param request PortletRequest

* @return NewEventxx2PortletSessionBean

*/

private static NewEventxx2PortletSessionBean getSessionBean(PortletRequest request) {

PortletSession session = request.getPortletSession();

if( session == null )

return null;

NewEventxx2PortletSessionBean sessionBean = (NewEventxx2PortletSessionBean)session.getAttribute(SESSION_BEAN);

if( sessionBean == null ) {

sessionBean = new NewEventxx2PortletSessionBean();

session.setAttribute(SESSION_BEAN,sessionBean);

}

return sessionBean;

}

/**

* Returns JSP file path.

*

* @param request Render request

* @param jspFile JSP file name

* @return JSP file path

*/

private static String getJspFilePath(RenderRequest request, String jspFile) {

String markup = request.getProperty("wps.markup");

if( markup == null )

markup = getMarkup(request.getResponseContentType());

return JSP_FOLDER + markup + "/" + jspFile + "." + getJspExtension(markup);

}

private static String getMarkup(String contentType) {

if( "text/vnd.wap.wml".equals(contentType) )

return "wml";

else

return "html";

}

/**

* Returns the file extension for the JSP file

*

* @param markupName Markup name

* @return JSP extension

*/

private static String getJspExtension(String markupName) {

return "jsp";

}

@ProcessEvent(name="name")

public void processEvent(EventRequest request,EventResponse response){

Event event = request.getEvent();

NewEventxx2PortletSessionBean sessionBean = getSessionBean(request);

if( sessionBean != null )

sessionBean.setFormText(((NewEventxxPortletSessionBean)event.getValue()).getFormText());

}

}

=========================================================

6.发布后添加信息连接

这一步如果不做的话 信息将不能传播。

使用RAD7.5开发Portlet的例子

相关推荐