How to Handle HTTP GET/POST using Portlet Events
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.
There are 2 advantages of using Portlet Events to handle GET/POST over the basic method:
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"/>’>
PortletAction
object and associate it with the PortletURI object created in
Step 1.eg
PortletAction storeAction = new DefaultPortletAction(“save”);
s1URI. addAction(storeAction);
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)
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
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);
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.
With all the basic concepts in place, implementing a Wizard is straightforward. Here is one possible solution:
FORM,
and create a PortletAction
each for each step.INPUT
fieldsPortletActions,
check which of the buttons is pressed. You can check this by looking
through the input fields from the portlet request object.request.setModeModifier(Portlet.ModeModifier.PREVIOUS)
to return the portlet to View mode.
request.setModeModifier(Portlet.ModeModifier.PREVIOUS);
to return the portlet to View mode.