14-Oct-06 (Created: 14-Oct-06) | More in 'Java-Portlets'

sample: processAction method

The processAction() method is invoked when the user submits one of the forms rendered by the portlet (by the doView() or the doEdit() method.)


public class WeatherPortlet extends GenericPortlet {
...
public void processAction(ActionRequest aReq, ActionResponse aRes)
throws PortletException, IOException {
  PortletPreferences prefs = aReq.getPreferences();
  String zip = aReq.getParameter("zip");
  if (aReq.getPortletMode().equals(PortletMode.VIEW)) {
    if (zip==null) {
       zip = prefs.getValue("zip","10000");
    }
    aRes.setRenderParameter("zip",zip);
  }
  else if (aReq.getPortletMode().equals(PortletMode.EDIT)) {
    boolean editOK;
    String errorMsg = null;
    String unit = aReq.getParameter("unit");
    prefs.setValue("zip",zip);
    prefs.setValue("unit",unit);
    try {
       prefs.store();
       editOK = true;
    }
    catch (ValidatorException ex) {
       editOK = false;
       errorMsg = ex.getMessage();
    }
    if (editOK) {
        aRes.setPortletMode(PortletMode.VIEW);
    }
    else {
        aRes.setRenderParameter("error",
        URLEncoder.encode(errorMsg));
    }
}
...
}

First, it obtains the PortletPreferences object. If the portlet is in VIEW mode, it checks if the zip code and units parameters are missing. If so, it sets the local variable to the default value. Finally, it sets the zip code as the render parameter, which will be received by subsequent invocations of the doView() method.