Portlet JSP Tutorial
Portlet

Tutorials

Basic

Hello World
Render Parameters
Action
Modes
JSP
Modes
Basic
 

Find this tutorial in: /usr/local/resin/webapps/resin-doc/portlet/tutorial/basic-jsp
Try the Tutorial

  1. Files in this tutorial
  2. Dispatching to a JSP for the view
  3. Functionality available in Portlet JSP's
  4. Flexible configuration

Files in this tutorial

WEB-INF/classes/example/HelloWorldPortlet.java Portlet
hello.jsp JSP that renders the view for HelloWorldPortlet
WEB-INF/web.xml web-app configuration

Dispatching to a JSP for the view

JSP is often the most convenient way to construct the view for a portlet. A portlet can dispatch to a jsp to provide the view in a fashion very similar to a servlet or a classic model-2 architecture like Struts.

The dispatch() method of the HelloWorldPortlet use the Portlet API to dispatch to a view, and the render() method does the dispatch.

dispatch() and render()
See it in: WEB-INF/classes/example/HelloWorldPortlet.java
  /**
   * Dispatch to a jsp or servlet.
   */
  protected void dispatch( RenderRequest request, 
                           RenderResponse response, 
                           String path )
    throws PortletException, IOException
  {
    PortletContext ctx = getPortletContext();
    PortletRequestDispatcher dispatcher = ctx.getRequestDispatcher(path);

    dispatcher.include(request, response);
  }

  public void render(RenderRequest request, RenderResponse response)
    throws PortletException, IOException
  {
    prepareObjects(request, response);
    dispatch(request, response, _view);
  }

Functionality available in Portlet JSP's

The standard JSP functionality, including JSTL and EL are available in a JSP that a portlet uses for the view.

Portlet request attributes, set in the portlet with request.setAttribute( "name", value ), are available in the JSP as request attributes request.getAttribute("name") and EL variables ${name}.

There is also a portlet JSP tag library that defines a small number of portlet specific tags. The most useful of the portlet tags are those that are provided for creating render and action urls.

portlet JSP taglibrary
See it in: hello.jsp

<%@ taglib uri="http://java.sun.com/portlet" prefix="portlet" %>

...

<portlet:actionUrl var="submitUrl" portletMode="edit"/>

...

<portlet:renderUrl var="editUrl" portletMode="edit"/>

Flexible configuration

The HelloWorldPortlet is written so that it provides a default path for the jsp that can be overriden by an init-param. This is a flexible architecture, if a different jsp page is desired for the view it can be specified without modifying the source code.

configure the portlet to use a different jsp
  <servlet servlet-name="hello" 
           servlet-class="com.caucho.portal.generic.PortletServlet">
    <init>
      <portlet resin:type="example.HelloWorldPortlet">
        <view>/custom/hello.jsp</view>
      </portlet>
    </init>

  </servlet>

Try the Tutorial


Modes
Basic
 
Copyright © 1998-2006 Caucho Technology, Inc. All rights reserved.
Resin® is a registered trademark, and HardCoretm and Quercustm are trademarks of Caucho Technology, Inc.