How to Handle HTTP GET/POST using Portlet Events

 

Introduction

In the previous tip, we learn how to handle HTTP GET/POST request in a portlet JSP by generating unique values for the action and input field names. This tip builds upon the previous tip by explaining how to use Portlet events to handle GET/POST requests.

 

Why do we need to use Portlet Events?

There are 2 advantages of using Portlet Events to handle GET/POST over the basic method:

  1. Code for handling the GET/POST request can be centralized in the event handler, rather than spread all over the individual doView(), doEdit() etc methods.
  2. It allows you to do more fancy stuff like closing the Edit mode and going back to View mode on submitting.
  3. GET/POST request initiated from one portlet can trigger a response in another portlet. This is also known as inter-portlet communication/messaging and will be covered in a future tip.

 

Steps

  1. Create the portlet’s URL for the FORM‘s action attribute. This is the same as in the previous tip.

eg

PortletURI s1URI = response.createURI()

request.setAttribute("actionVal", s1URI.toString());

 

            You would also setup your FORM in your JSP as described in the previous tip.

eg

            <jsp:useBean id="actionVal" class="java.lang.String" scope="request" />

<FORM method='POST' action='<%=actionVal%>'>

      <input type="text" name=’<portletAPI:encodeNamespace value="firstpage"/>’>

 

  1. Create a PortletAction object and associate it with the PortletURI object created in Step 1.

eg

PortletAction storeAction   = new DefaultPortletAction(save”);

s1URI. addAction(storeAction);

 

  1. Create a event handler class

If you have subclassed from the AbstractBridgePortletController class for your portlet class, it is already the event handler. You just need to override the doActionEvent() method with the event handling code.

 

If not, you need to create a handler class by subclassing from the ActionAdapter class and overridding the actionPerformed() method with the event handling code. Register this class in the portlet.xml file under the <listener> element.

eg

<listener>

<listener-class type="action">

com.ibm.etc.wps.portlets.firstpage.FirstPageActionListener

</listener-class>

</listener>

 

The event handler will be called when the PortletURI created in Step 1, and added with the action in Step 2, is requested by the browser (ie when user submit the FORM)

 

  1. In your event handler method, implement the event handling code. You can check which event is being triggered by looking into the ActionEvent object that is passed in. You can also retrieve the portlet request object from the event object to read your INPUT fields.

eg

public void actionPerformed (ActionEvent event) {

 

PortletAction _action = event.getAction();

       

if (_action instanceof DefaultPortletAction) {

    PortletRequest request = event.getRequest();

    DefaultPortletAction action = (DefaultPortletAction)_action;      

    if (action.getName().equals("save")) {

       String firstpage  = request.getParameter(“firstpage”);

       //do something appropriate, eg save the firstpage value

        

        

    } else if(action.getName().equals("someOtherAction")){

        //do something else

        

        

    }

 

More Fancy Stuffs

 

How do I implement a “Save and Close” button in Edit mode?

In step 1, instead of calling createURI() method to create the PortletURI object, call createReturnURI(). This will return the portlet to the previous mode when the action is invoked.

 

Alternatively, you can call the setModeModifier() method of the PortletRequest in the event handler code:

eg

if (_action instanceof DefaultPortletAction) {

    PortletRequest request = event.getRequest();

    DefaultPortletAction action = (DefaultPortletAction)_action;      

    if (action.getName().equals("save")) {

       String firstpage  = request.getParameter(“firstpage”);

       //do something appropriate, eg save the firstpage value

        

       // return to previous mode

       request.setModeModifier(Portlet.ModeModifier.PREVIOUS);

 

 

How do I implement a “Cancel” button?

 

Just like as explained above, but it’s even simpler. Use the createReturnURI() method to create the PortletURI object. In the handler code, do nothing for this event. In fact, you do not event need to create a PortletAction for this PortletURI since there is nothing to be done.

 

How do I implement an Edit Wizard?

 

With all the basic concepts in place, implementing a Wizard is straightforward. Here is one possible solution:

  1. Implement each step of the Wizard as a FORM, and create a PortletAction each for each step.
  2. Implement the “Back”,”Next”, “Finish”, “Cancel” buttons as submit buttons
  3. Implement the values the user enters as INPUT fields
  4. In the event handler, for each of the PortletActions, check which of the buttons is pressed. You can check this by looking through the input fields from the portlet request object.
    1. Back button: Store the input values in a scratch area, and display the previous step’s JSP. (Be careful about using instance variables to implement your scratch area. See the Portlet Development Guide for details on use of instance variables)
    2. Next button: Store the input values in a scratch area, and display the next steps’ JSP.
    3. Cancel button: Do not store input values. Call

request.setModeModifier(Portlet.ModeModifier.PREVIOUS)

                        to return the portlet to View mode.

    1. Finish button: Store values from scratch area to Portlet Data. Call

request.setModeModifier(Portlet.ModeModifier.PREVIOUS);

                        to return the portlet to View mode.

Hosted by www.Geocities.ws

1