Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/spring-mvc/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何捕获和处理org.springframework.web.portlet.bind.MissingPortletRequestParameterException_Spring_Spring Mvc_Portlet - Fatal编程技术网

如何捕获和处理org.springframework.web.portlet.bind.MissingPortletRequestParameterException

如何捕获和处理org.springframework.web.portlet.bind.MissingPortletRequestParameterException,spring,spring-mvc,portlet,Spring,Spring Mvc,Portlet,这是我的一个控制器方法 @ResourceMapping(value="petPortalAction") @RequestMapping(params={"transactionType=BUY_PET"}) public String handlePetPurchaseAction( @RequestParam(required=true, value="petId") String petId,

这是我的一个控制器方法

  @ResourceMapping(value="petPortalAction")
  @RequestMapping(params={"transactionType=BUY_PET"})
  public String handlePetPurchaseAction(
                    @RequestParam(required=true, value="petId") String petId, 
                    PortletRequest request,
                    Model model)
  {
  ... 
  }
如您所见,参数petId是必需的请求参数。 如果请求中缺少该参数,将引发org.springframework.web.portlet.bind.MissingPortletRequestParameterException

我的问题是如何捕获和处理此异常。。。在spring中有我可以使用的监听器吗


提前谢谢。

这个问题有点老了,但我也遇到了同样的问题(这个问题在谷歌上出现得很高;-)。以下是我所做的: 我在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" />

错误/错误缺少参数
error/error missing参数是要显示的视图的名称。Spring将自动将异常发布到视图中,默认情况下调用变量“exception”。在一个典型的JSP设置中,根据您的视图解析器配置,我将view error/error missing参数映射到/WEB-INF/JSP/error/error-missing-parameter.JSP。例如,此文件可以简单地如下所示:

<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>

缺失参数
错误:缺少类型为“”的参数“”

如果下面的exceptionMappings都不匹配,则属性“defaultErrorView”将映射到视图。如果不需要此属性,则可以跳过此属性