PDA

View Full Version : Problem with use of ActionResponse.sendRedirect in Spring Portlet



dbames
Jun 13th, 2008, 04:40 AM
We have a portlet application using Spring 2.0.8 and running on Oracle Application Server.

We are trying to use the ActionResponse.sendRedirect method in the processFormSubmission method of the controller in order to redirect users out of the portlet altogether if a specific condition is met:

However, whenever the sendRedirect method is invoked, we receive the following error:


Method sendRedirect() cannot be called after method setPortletMode()

As can be seen from the controller snippet below, we are not setting the portlet mode anywhere in the processFormSubmission method of the controller:


protected void processFormSubmission(ActionRequest actionRequest, ActionResponse actionResponse, Object object, BindException bindException) throws SystemException {
try {
// Get the form that was posted
WelcomeForm welcomeForm = (WelcomeForm) object;
//Check if the customer is out of scope of the application
if (welcomeForm.getAreYouOutOfScope().equalsIgnoreCas e("yes")) {
actionResponse.sendRedirect("http://www.google.com");
return;
}
} catch (SystemException se) {
throw se;
} catch (Exception e) {
throw new SystemException(ExceptionMessage.UNEXPECTED_EXCEPT ION, e);
}
}

I have looked throught the API documentation to try and see if there are any issues with the use of the sendRedirect method and came across a reference in the for the ParameterMappingInterceptor class that states that you cannot use sendRedirect if this interceptor is used, however, we are not using this interceptor.

I wondered whether use of the PortletModeParameterHandlerMapping would cause the portlet mode to be set and have amended the code to use the ParameterHandlerMapping class instead - however, the same problem still occurs. Example configuration below:


<bean id="portletParameterHandlerMapping" class="org.springframework.web.portlet.handler.ParameterH andlerMapping">
<property name="parameterName" value="action"/>
<property name="parameterMap">
<map>
<entry key="welcome"><ref bean="welcomeController"/></entry>
</map>
</property>
</bean>

Can anyone advise as to why the sendRedirect method is resulting in an exception? Thanks in advance for any help.

johnalewis
Jun 13th, 2008, 05:38 PM
Since you are referencing the processFormSubmission method, this means you are working with either a SimpleFormController or an AbstractFormController. If you are working with SimpleFormController, there is a good chance you should be using one of the onSubmitAction methods or doSubmitAction, since they only get run after successful binding.

Regardless, the issue you are seeing is caused by AbstractFormController/SimpleFormController because it uses render parameters to communicate state between the action phase and the render phase.

If you are planning to issue a redirect once the form is successfully submitted, you need to set the property 'redirectAction' to true on your controller. Here is the description of this property from the AbstractFormController javadoc:

Name: redirectAction
Default:false
Description: Specifies whether processFormSubmission is expected to call ActionResponse.sendRedirect. This is important because some methods may not be called before ActionResponse.sendRedirect (e.g. ActionResponse.setRenderParameter). Setting this flag will prevent AbstractFormController from setting render parameters that it normally needs for the render phase. If this is set true and sendRedirect is not called, then processFormSubmission must call setFormSubmit. Otherwise, the render phase will not realize the form was submitted and will simply display a new blank form.

Hope that helps!

dbames
Jun 16th, 2008, 05:21 AM
Thanks very much for your help. Setting the redirectAction property to true on the controller implementation (which is based on AbstractFormController) made the ActionReponse.sendRedirect work.

That'll teach me to read the API documentation properly !

Thank you again.