Here is one of my controller method,
@ResourceMapping(value="petPortalAction")
@RequestMapping(params={"transactionType=BUY_PET"})
public String handlePetPurchaseAction(
@RequestParam(required=true, value="petId") String petId,
PortletRequest request,
Model model)
{
...
}
As you can see, parameter petId is mandatory request parameter. If it's missing from the request, org.springframework.web.portlet.bind.MissingPortletRequestParameterException will be raised.
My question is how to catch and handle this exception...开发者_如何学运维 Is there any listener in spring that I can use?
Thanks in advance.
The question is somewhat old, but I had the same problem (and this question comes up pretty high on Google ;-)). So here is what I did: I added the follwing to my applicationContext.xml:
<bean id="defaultExceptionHandlerTemplate" class="org.springframework.web.portlet.handler.SimpleMappingExceptionResolver" abstract="true">
<property name="defaultErrorView" value="error/error-exception-default" />
<property name="exceptionMappings">
<props>
<prop key="org.springframework.web.portlet.bind.MissingPortletRequestParameterException">
error/error-missing-parameter
</prop>
</props>
</property>
</bean>
<bean id="defaultExceptionHandler" parent="defaultExceptionHandlerTemplate" />
The error/error-missing-parameter is the name of a view that is to be displayed. Spring will automatically publish the exception to the view, by default calling the variable "exception". On a typical JSP setup that I have the view error/error-missing-parameter maps to /WEB-INF/jsp/error/error-missing-parameter.jsp depending on your view resolver configuration. This file can for example simply look like this:
<jsp:root version="2.0"
xmlns:jsp="http://java.sun.com/JSP/Page"
xmlns:c="urn:jsptld:http://java.sun.com/jsp/jstl/core">
<h1>Missing parameter</h1>
<p>Error: The parameter '<c:out value="${exception.parameterName}" />' of type '<c:out value="${exception.parameterType}" />' is missing.</p>
</jsp:root>
The property "defaultErrorView" maps to a view in case that none of the exceptionMappings below matches. If you don't need this, than you can skip this property.
精彩评论