intra-mart Accel Platform / Portlet Programming Guide

«  Portlet Development (SA Struts Edition)   ::   Contents   ::   Portlet Development (Asynchronous Portlet Edition)  »

Portlet Development (Spring Edition)

Summary

In the Spring portlet, screen display process is created using Spring Framework.
It can be created in a similar way Spring Framework portlet is developed.
In order to realize the portlet lifecycle, @RenderMapping,  @ActionMapping, and @EventMapping annotations are used.
For details about Spring Portlet MVC Framework, please refer to http://docs.spring.io/spring/docs/3.2.4.RELEASE/spring-framework-reference/html/portlet.html.
  1. Screen Development (render cycle)

    Screen which is displayed on the portlet is created.  It can be switched by looking at mode and window status.
    Screen for the display mode is always necessary.
  2. Action Process (processAction cycle)

    Data process submitted from the portlet is created. It can be used by setting @ActionMapping annotation to the Controller class method.
    It is also possible to make a linkage with other portlet by setting the Event.
  3. Event Process (processEvent cycle)

    Receive Process of the event generated by other portlet is created. It can be used by setting @EventMapping annotation to the
    Controller class method.
    It will be executed only when Event is set in the Action process.
Annotation which is set to the method varies for each lifecycle of the portlet as shown below.
render mandatory @RenderMapping
processAction optional @ActionMapping
processEvent optional @EventMapping
In the subsequent sections APIs which are available on the portlet and its implementation method in each lifecycle are briefly explained.

Difference from Portlet Development in Spring Portlet MVC Framework

Major differences are listed below.
  • portlet.xml
    In the Spring Portlet MVC Framework, <portlet> is added to portlet.xml for each portlet.
    However, no individual registration is made in intra-mart Accel Platform.
    Portlet will be operated based on the <portlet-name>SpringPortlet</portlet-name> setting of WEB-INF/portlet.xml.
    Value that was set in [URL of Portlet New Registration/Edit Screen] is set to portlet-name.
  • Naming Rule and Location of xml File of context of portlet
    It is classpath:META-INF/spring/[URL of Portlet New Registration/Edit Screen]-portlet.xml.
    Please refer to [xml File of portlet context] for detail.
  • JSP
    Please be aware that there are some remarks like HEAD tag or BODY tag cannot be used.
    Please refer to [Remarks when Screen is Created] for detail.

Portlet API

Portlet with just a simple screen display can be created without using the Portelt API.  However, if you want to fully utilize the functions of portlet,
it is necessary to utilize the Portlet API.
Especially, for the Action process and Event process to be executed, Portlet API must be used.
the Action process and Event process to be executed, Portlet API must be used.
This document describes only the basic functions.
Please refer to [Portal API List] and [Portlet API2.0 Document] for information about all APIs.

You can get Portlet API2.0 document from below.

PortalManager

While PortletAPI2.0 can be used as is in JavaEE Development Model, PortalManager class is provided as an accessor to use these APIs more easily.
jp.co.intra_mart.foundation.portal.common.PortalManager
For each of the functions in PortalManager, it will be described when it is actually used in the subsequent sections.

Portlet Mode

Portlet modes available are View Mode (VIEW) and Edit Mode (EDIT).

Obtaining Portlet Mode

Current portlet mode is obtained as shown below using the API.
As the portlet mode, constant of [javax.portlet.PortletMode] class as below is obtained.

(ex.) PortletMode.VIEW, PortletMode.EDIT

javax.portlet.RenderRequest renderRequest = PortalManager.getRenderRequest();
javax.portletPortletMode portletMode = renderRequest.getPortletMode();

Window Status

Obtaining Window Status

Current portlet mode is obtained as shown below using the API.
As the window status, constant of [javax.portlet.WindowState] class as below is obtained.
Since portlet is not called in the minimized status, [WindowState.MINIMIZED] will not be obtained.

(ex.) WindowState.NORMAL, WindowState.MAXIMIZED, WindowState.MINIMIZED

javax.portlet.RenderRequest renderRequest = PortalManager.getRenderRequest();
javax.portlet.WindowState windowState = renderRequest.getWindowState();

xml File of portlet context

Naming rule and the placement location of the file name of xml file  of context of Spring Portlet is as follows.
  • File Name
    It should be created in [URL of Portlet New Registration/Edit Screen]-portlet.xml format.
    (ex.) If you set [welcome] to the URL of Portlet New Registration/Edit Screen, it will be welcome-portlet.xml.
  • Placement Location
    In the module project, please create [META-INF/spring] folder in src/main/resources, and place the xml file.
    (ex.) src/main/resources/META-INF/spring/welcome-portlet.xml
Please set the Controller class to xml so that it becomes the subject of component scan.
<context:component-scan base-package="Controller Class Package" />

Screen Development (render cycle)

In the render cycle screen is displayed using JSP as view, which is similar to the way normal Web page is displayed.
Screen development of Spring Development Model is basically not different from the development of normal Spring Portlet MVC Framework  screens.
URL which is set on the Portlet New Registration/Edit Screen (Refer to [Portal Group Administrator Operations Guide]) corresponds to the portlet name
of normal portlet development (portlet-name part of portlet.xml in normal portlet development.)

Controller Class

Create the Controller Class which sets @Controller annotation and @RequestMapping annotaion, and
create the method which sets @RenderMapping annotation.
Portlet mode (VIEW or EDIT) will be specified in @RequestMapping.
Set @RenderMapping annotation to te method which processes render.
Please do the setting as @RenderMapping(params = “action=list”), if you want to select the method to process render by the parameter.
@Controller
@RequestMapping("VIEW")
public class ViewController {

    // default render
    @RenderMapping
    public String view() {
        return "welcome/view.jsp";
    }

    // render that corresponds to renderURL.setParameter("action", "list")
    @RenderMapping(params = "action=list")
    public String list() {
        return "welcome/list.jsp";
    }
}

JSP File

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

<%@ page import="jp.co.intra_mart.foundation.portal.common.PortalManager" %>
<%@ page import="javax.portlet.PortletURL" %>

<%
PortletURL renderURL = PortalManager.createRenderURL();
%>
Corresponding to @RenderMapping.
<a href="<%=renderURL.toString() %>">default render</a>
<br/>

<%
PortletURL renderURL2 = PortalManager.createRenderURL();
renderURL2.setParameter("action", "list");
%>
Corresponding to @RenderMapping(params = "action=list").
<a href="<%=renderURL2.toString() %>">render (action=list)</a>
<br/>

RenderRequest, RenderResponse

Unlike the requests of normal Web pages, request object and response object which are used by the portlet will use RenderRequest object and
RenderResponse object which are the dedicated objects for displaying portlet.
Information of portlet can be obtained by using these objects.
  • Obtaining RenderRequest and RenderResponse

    RenderRequest renderRequest = PortalManager.getRenderRequest();
    RenderResponse renderResponse = PortalManager.getRenderResponse();
    
  • Obtaining Page Argument in JSP

    <%
      RenderRequest renderRequest = PortalManager.getRenderRequest();
      String value = renderRequest.getParameter("param1");
    %>
    

    Current portlet mode and window status are obtained from RenderRequest.

Scope of RenderRequest

Scope of RenderRequest is different from the scope of request of normal Web application.
All the portlets retain requests independently, and request parameters for displaying portal screen are not inherited.
However, following parameters are set automatically by the portlet container.
portal_cd Key of portal screen where portlet is placed.
portalKind

Portal type of portal screen where portlet is placed. Following values are obtained.

user User Portal
group Group Portal
In order to set the request parameters to the portlet, it is necessary to execute Action process or Event process.
Request parameters that were once set will be valid during the session duration until the next Action process or Event process is executed.

ActionURL, RenderURL

In case you create the function in which Submit process is done from the portlet and the Portal Screen is displayed again after the process,
submission to the following URLs would be necessary to communicate with the portlet container.
ActionURL URL to call Action function
RenderURL URL to re-display Portal Screen
You can obtain them by using PortalManager.
  • Obtaining ActionURL and RenderURL

    PortletURL actionURL = PortalManager.createActionURL();
    PortletURL renderURL = PortalManager.createRenderURL();
    
Shown below is the sample program to call Action process after the submission by the portlet.
  • JSP

    <%
      PortletURL actionURL = PortalManager.createActionURL();
    %>
    <!-- Call service that performs some registration process -->
    <form action="<%= actionURL.toString() %>" method="POST">
    <!-- Value which is used in Registration Process -->
      <input type="text" name="param1" value="">
      <input type="submit" value="Execute">
    </form>
    
    • Only way to call the Action Process is to submit to ActionURL.
    • Since ActionURL includes portal information, data volume would be large.  Please specify POST for method.
    In case Action Process is called by specifying parameters.
    <%
      PortletURL actionURL = PortalManager.createActionURL();
      actionURL.setParameter("action", "register")
    %>
    <!-- Call service that performs some registration process -->
    <form action="<%= actionURL.toString() %>" method="POST">
    <!-- Value which is used in Registration Process -->
      <input type="text" name="param1" value="">
      <input type="submit" value="Execute">
    </form>
    
    • You can set parameters by using setParameter, and call Action process.
       In this case, it will be [action=register].

Action Process (processAction cycle)

In the Action process, @ActionMapping annotation is set to the method of Controller Class.
Action Process is called and executed by submitting to the Action URL from the screen created by JSP.
In the Action process, setting of RenderParamter, setting of PortletPreference, and update process such as event setting are made.

Controller Class

Controller Class which sets @Controller annotation and @RequestMapping annotation is created, and the method which sets
@ActionMapping annotation is created.
Portlet mode (VIEW and EDIT) is specified to @RequestMapping.
import jp.co.intra_mart.foundation.portal.common.handler.ImEvent;

@Controller
@RequestMapping("VIEW")
public class ViewController {

    // default render
    @RenderMapping
    public String view() {
        return "welcome/view.jsp";
    }

    // render thaht corresponds to renderURL.setParameter("action", "list")
    @RenderMapping(params = "action=list")
    public String list() {
        return "welcome/list.jsp";
    }

    // default action
    @ActionMapping
    public void register(@RequestParam("name") String name, ActionResponse response) {
        // Registration Process ...

        // Transition to the render of @RenderMapping(params="action=list")
        response.setRenderParameter("action", "list");
    }

    // action that corresponds to actionURL.setParameter("action", "publish")
    // action that generates event
    @ActionMapping(params = "action=publish")
    public void publish(ActionRequest request, ActionResponse response) {
        // Event Generation
        ImEvent event = new ImEvent();
        event.setEvent("hello");
        response.setEvent(ImEvent.IM_QNAME, event);
    }
}

JSP File

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

<%@ page import="jp.co.intra_mart.foundation.portal.common.PortalManager" %>
<%@ page import="javax.portlet.PortletURL" %>

<%
PortletURL renderURL = PortalManager.createRenderURL();
%>
@RenderMapping is supported.
<a href="<%=renderURL.toString() %>">default render</a>
<br/>

<%
PortletURL renderURL2 = PortalManager.createRenderURL();
renderURL2.setParameter("action", "list");
%>
@RenderMapping(params = "action=list") is supported.
<a href="<%=renderURL2.toString() %>">render (action=list)</a>
<br/>

<%
PortletURL actionURL = PortalManager.createActionURL();
%>
@ActionMapping is supported.
<form method="post" action="<%=actionURL.toString() %>">
	<input type="text" name="name" value="">
	<button type="submit">Register</button>
</form>
<br/>

<%
PortletURL actionURL2 = PortalManager.createActionURL();
actionURL2.setParameter("action", "publish");
%>
@ActionMapping(params = "action=publish")is supported.
<a href="<%=actionURL2.toString() %>">Event Generation</a>
<br/>

ActionRequest, ActionResponse

Arguments of Action Handler are ActionRequest object and ActionResponse object, which are the dedicated object for Action process.
By using these objects, request argument can be obtained and event setting can be made.
Please refer to [Portlet API2.0 Document] for detail.

RenderParameter Setting

Request parameter (RenderParameter), which can be obtained when the screen is displayed, is set in the Action process or Event process.
Request parameter at the time of Action process call is stored in ActionRequest, but it is not inherited to render cycle. Therefore, it should
be explicitly set if it is required.
Please note that all the RenderParameter that were previously set are cleared when Action process is called.

(ex.) RenderParameter setting in processAction cycle

// ActionResponse responose
// In case request parameter is set
response.setRenderParameter("param1", "value1");
// In case request parameter is deleted
response.setRenderParameter("param2", null);

(ex.) Use of RenderParameter in render cycle

String value = request.getParamter("param1");

PortletPreferences Setting

PortletPreferences is a storage area of portlet information for each user.
In the portlet container of intra-mart Accel Platform, information which was set in PortletPreferences is saved to the database.
Information which is set here can be accessed anytime when the screen is displayed.

(ex.) Use of javax.portlet.PortletPreferences

// Obtaining PortletPreferences
PortletPreferences preferences = request.getPreferences();
// Obtaining Information which was set in PortletPreferences
String value = preferences.getValue("key1", "Default Value");
// Information is set to PortletPreferences.
preferences.setValue("key1", "value1");
// PortletPreferences is settled.
//  Unless this process is done, information is not saved.
preferences.store();

Event Setting

By setting the event you can notify the event occurrence to multiple portlets other than the portlet that accepted the Action process and pass the process.
Please set jp.co.intra_mart.foundation.portal.common.handler.ImEvent.IM_QNAME to the event id.

(ex.) Event Setting in processAction cycle

@ActionMapping(params = "action=publish")
public String publish(ActionRequest request, ActionResponse response) {
    ImEvent event = new ImEvent();
    event.setEvent("hello");
    response.setEvent(ImEvent.IM_QNAME, event);
}

Event Process (processEvent cycle)

Since there is no screen displayed in the processEvent cycle, only the Event Handle class which implements PortletEventHandler interface is created.
Event process will be executed by further registering the Event Handler created on the portlet administration screen and
setting the event by the Action process of any portlet.

In the Event process, setting of RenderParamter, setting of PortletPreferences, and update process such as event setting are made.

Controller Class

Controller Class which sets @Controller annotation and @RequestMapping annotation is created, and the method which sets
@EventMapping annotation is created.
Portlet Mode (VIEW or EDIT) is specified in @RequestMapping.
Please specify @EventMapping(“ImEvent”) and ImEvent to @EventMapping.
import jp.co.intra_mart.foundation.portal.common.handler.ImEvent;

@Controller
@RequestMapping("VIEW")
public class AnotherController {

    // default render
    @RenderMapping
    public String view() {
        return "receive/view.jsp";
    }

    // event
    @EventMapping("ImEvent")
    public void event(EventRequest request, EventResponse response) {
        Event event = request.getEvent();
        ImEvent imEvent = (ImEvent) event.getValue();
        if (!"hello".equals(imEvent.getEvent())) {
            // Process is not executed except for the "hello" event.
            return;
        }
        // Some Process ...
    }
}

ImEvent Object

With regard to the Event object which can be obtained from EventRequest, ImEvent object will be set between intramart portlets.
Following information is stored in ImEvent object.
Property Name Type Value
id String
Event ID which has been set in the Action Process
If omitted, character string [ImEvent]  is set.
value String
Value of Event which has been set in the Action Process
It is obtained as a character string when object is set too.
source String Portlet code of portlet which has set the Event.

EventRequest, EventResponse

Arguments of Event Handler are the EventRequest object and EventResponse object, which are the dedicated objects for event process.
You can obtain request arguments and set the events by using these objects.
Please refer to [Portlet API2.0 Document] for detail.

RenderParameter Setting

Request parameter which can be obtained when the screen is displayed (RenderParameter) is set in the Action process or in the Event process.
RenderParameter which was already set when the event process was called is stored in EventRequest, and it is automatically inherited to the render cycle
unless any special process is made.
If it is not necessary to inherit it, it should be deleted explicitly.

(ex.) Setting of RenderParameter in processEvent cycle

// In case request parameter is set.
response.setRenderParameter("param1", "value1");
// In case request parameter is deleted.
response.setRenderParameter("param2", null);

(ex.) Use of RenderParameter in render cycle

request.getParamter(eventId);

PortletPreferences Setting

PortletPreferences is the storage area of portlet information for each user.
In the portlet container of intra-mart Accel Platform, information that was set in PortletPreferences is saved in the database.
Information which was set here can be accessed anytime when the screen is displayed.

(ex.) Use of java.portlet.PortletPreferences

// Obtaining PortletPreferences
PortletPreferences preferences = request.getPreferences();
// Obtaining Information which was set in PortletPreferences
String value = preferences.getValue("key1", "Default Value");
// Set Information to PortletPreferences.
preferences.setValue("key1", "value1");
// PortletPreferences is settled.
//  If this process is not done, information is not stored.
preferences.store();

Event Setting

By setting the event, you can notify the event occurrence to multiple portlets other than the portlet that accepted the Action process and pass the process.
However, please be aware that doing the event process within the event process would cause performance degradation, and may possibly cause the event loop.
So, it should be implemented carefully.

(ex.) Event Setting in processEvent cycle

response.setEvent(eventId, eventValue);

«  Portlet Development (SA Struts Edition)   ::   Contents   ::   Portlet Development (Asynchronous Portlet Edition)  »