如何捕获NoSuchFlowDefinitionException

时间:2014-04-21 14:13:33

标签: spring-mvc exception-handling error-handling

每次抛出NoSuchFlowDefinitionException异常时,我都要做额外的处理。任何想法如何做到这一点?

我们在应用程序中有3个servlet过滤器,但不知道在哪里添加此处理。完全是Spring Web流程的新手。

Spring配置

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xmlns:flow="http://www.springframework.org/schema/webflow-config"
   xmlns:p="http://www.springframework.org/schema/p"
   xmlns:jee="http://www.springframework.org/schema/jee"
   xmlns:context="http://www.springframework.org/schema/context"
   xmlns:jdbc="http://www.springframework.org/schema/jdbc"

 xsi:schemaLocation="http://www.springframework.org/schema/beans
                     http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
         http://www.springframework.org/schema/context
             http://www.springframework.org/schema/context/spring-context-2.5.xsd
                     http://www.springframework.org/schema/webflow-config
              http://www.springframework.org/schema/webflow-config/spring-webflow-config-2.0.xsd
        http://www.springframework.org/schema/jdbc 
                    http://www.springframework.org/schema/jdbc/spring-jdbc-3.0.xsd
            http://www.springframework.org/schema/jee   
                    http://www.springframework.org/schema/jee/spring-jee.xsd">

<!-- Spring Configuration -->
<bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
    <property name="mappings">
        <value>
            /*.do=flowController
            /mvc/consent=consentTestController
       <!-- /projects/npdBridge=npdController commenting out npd controller -->
            /projects/*=flowController
            /mvc/pixels=respondentTrackerController
            /mvc/loadingimages=loadingImageTestController
            /mvc/cookiefeature=CookieFeatureController
            /mvc/cookieinfo=CookieInfoController
            /mvc/status=statusController
            /mvc/clearConfigurationCache = clearConfigurationCacheController
        </value>
    </property>
    <property name="alwaysUseFullPath" value="true"/>
</bean>



<!-- Maps a logical view name to a physical resource (using tiles)-->
<bean id="viewResolver" class="org.springframework.js.ajax.AjaxUrlBasedViewResolver">
    <property name="viewClass" value="org.springframework.webflow.mvc.view.FlowAjaxTilesView"/>
</bean>

<bean id="tilesConfigurer"     class="org.springframework.web.servlet.view.tiles2.TilesConfigurer">
    <property name="definitions">
        <list>
            <value>/WEB-INF/tiles/tiles-definitions.xml</value>
        </list>
    </property>
</bean>

<bean id="messageSource" class="com.libs.webflowUtil.TranslatorMessageSource">
    <constructor-arg>
        <bean class="org.springframework.beans.factory.config.PropertyPathFactoryBean">
          <property name="targetBeanName"><value>applicationResources</value></property>
          <property name="propertyPath"><value>translator</value></property>
        </bean>
    </constructor-arg>
</bean>
<bean id="staticResourcesServerName"
        factory-bean="applicationResources"
        factory-method="getStaticResourcesServername">

</bean>



<!-- Webflow configuration -->
<bean id="flowController" class="org.springframework.webflow.mvc.servlet.FlowController">
    <property name="flowExecutor" ref="flowExecutor"/>
</bean>

<bean id="consentTestController" class="com.apps.abcFlowController.subpanel.MembershipConsentViewController">
    <constructor-arg><ref bean="applicationResources"/></constructor-arg>
</bean>

<bean id="npdController" class="com.apps.abcFlowController.clientSpecific.npd.BridgeController">
    <constructor-arg><ref bean="applicationResources"/></constructor-arg>
</bean>

<bean id="respondentTrackerController" class="com.apps.abcFlowController.respondent.RespondentTrackerController">
    <constructor-arg><ref bean="applicationResources"/></constructor-arg>
</bean>

<bean id="loadingImageTestController" class="com.apps.abcFlowController.subpanel.LoadingImageTestController">
    <constructor-arg><ref bean="applicationResources"/></constructor-arg>
</bean>

<bean id="CookieInfoController" class="com.apps.abcFlowController.respondent.CookieInfoController">
    <constructor-arg><ref bean="applicationResources" /></constructor-arg>
</bean>

<bean id ="clearConfigurationCacheController" class ="com.apps.abcFlowController.util.CacheController">
    <constructor-arg><ref bean="applicationResources" /></constructor-arg>
</bean>

<bean id="CookieFeatureController" class="com.apps.abcFlowController.respondent.CookieFeatureController">
    <constructor-arg><ref bean="applicationResources" /></constructor-arg>
</bean>

<bean id="statusController" class="com.apps.abcFlowController.status.StatusController">
    <constructor-arg><ref bean="applicationResources" /></constructor-arg>
</bean>

<!-- Add profiling support for flow transitions -->
<bean id="profilingFlowExecutionListener" class="com.libs.abcFlowControllerProfiling.collect.ProfilingFlowExecutionListener" />
 <!--  listener for static item references -->
<bean id="viewListener" class="com.apps.abcFlowController.ViewListener">
    <constructor-arg><ref bean="staticResourcesServerName"/></constructor-arg>
    </bean>





<flow:flow-executor id="flowExecutor" flow-registry="flowRegistry">
    <flow:flow-execution-listeners>
         <flow:listener ref="viewListener" />
    </flow:flow-execution-listeners>
    <flow:flow-execution-repository max-executions="5"/>
</flow:flow-executor>


<!-- Register flows. -->
<flow:flow-registry id="flowRegistry" flow-builder-services="flowBuilderServices">

    <!-- Dynamix flows. -->
    <flow:flow-location path="/WEB-INF/flows/lifecycle/sae.xml" id="start"/>
    <flow:flow-location path="/WEB-INF/flows/lifecycle/sae.xml" id="estart"/>
    <flow:flow-location path="/WEB-INF/flows/lifecycle/sae.xml" id="eset"/>
    <flow:flow-location path="/WEB-INF/flows/lifecycle/sae.xml" id="prefine"/>
    <flow:flow-location path="/WEB-INF/flows/lifecycle/sae.xml" id="plist"/>
    <flow:flow-location path="/WEB-INF/flows/lifecycle/sae.xml" id="pno"/>
    <flow:flow-location path="/WEB-INF/flows/lifecycle/sae.xml" id="pstart"/>
    <flow:flow-location path="/WEB-INF/flows/lifecycle/sae.xml" id="sae_i"/>
    <flow:flow-location path="/WEB-INF/flows/lifecycle/sae.xml" id="fb"/>
    <flow:flow-location path="/WEB-INF/flows/lifecycle/sae.xml" id="sae_e"/>
    <flow:flow-location path="/WEB-INF/flows/lifecycle/sae.xml" id="sae_p"/>
    <flow:flow-location path="/WEB-INF/flows/lifecycle/sae.xml" id="sae_g"/>
    <flow:flow-location path="/WEB-INF/flows/lifecycle/sae.xml" id="end"/>
    <flow:flow-location path="/WEB-INF/flows/lifecycle/sae.xml" id="ext"/>
    <flow:flow-location path="/WEB-INF/flows/lifecycle/sae.xml" id="boomerang"/>

    <!-- New aliases for flows. -->
    <flow:flow-location path="/WEB-INF/flows/lifecycle/sae.xml" id="internal"/>
    <flow:flow-location path="/WEB-INF/flows/lifecycle/sae.xml" id="facebook"/>
    <flow:flow-location path="/WEB-INF/flows/lifecycle/sae.xml" id="web"/>
    <flow:flow-location path="/WEB-INF/flows/lifecycle/sae.xml" id="preferred"/>
    <flow:flow-location path="/WEB-INF/flows/lifecycle/sae.xml" id="general"/>

    <flow:flow-location path="/WEB-INF/flows/error/error.xml" />

    <!-- SAE subflows. -->
    <flow:flow-location path="/WEB-INF/flows/lifecycleStep/assimilate.xml"/>
    <flow:flow-location path="/WEB-INF/flows/lifecycleStep/dispatch.xml"/>
    <flow:flow-location path="/WEB-INF/flows/lifecycleStep/validate.xml"/>
    <flow:flow-location path="/WEB-INF/flows/lifecycleStepComponent/TrackingBeacons.xml"/>
    <flow:flow-location path="/WEB-INF/flows/lifecycleStepComponent/identify.xml"/>
    <flow:flow-location path="/WEB-INF/flows/lifecycleStepComponent/relevantID.xml"/>
    <flow:flow-location path="/WEB-INF/flows/lifecycleStepComponent/verify.xml"/>

</flow:flow-registry>


<flow:flow-builder-services id="flowBuilderServices" conversion-service="conversionService" view-factory-creator="viewFactoryCreator"/>


<jee:jndi-lookup id="applicationResources" jndi-name="java:app/abc/ApplicationResources"/>


<bean id="conversionService" class="com.apps.abcFlowController.binding.ConversionService">
    <constructor-arg><ref bean="applicationResources"/></constructor-arg>
</bean>

<bean id="viewFactoryCreator" class="org.springframework.webflow.mvc.builder.MvcViewFactoryCreator">
    <property name="viewResolvers">
        <list><ref bean="viewResolver"/></list>
    </property>
</bean>

<!-- abcFlowController Configuration -->
<bean class="com.libs.webflowUtil.LoggingExceptionResolver">
    <property name="defaultErrorView" value="error"/>
    <property name="defaultStatusCode" value="500"/>
</bean>

<bean id="webflowURLConverter" class="com.libs.webflowUtil.WebflowURLConverter" />

由于

2 个答案:

答案 0 :(得分:0)

您可以从this博客文章中获取Spring MVC异常处理的所有详细信息。

如果要全局处理NoSuchFlowDefinitionException(对于每个Spring MVC控制器都是这样),请执行以下操作:

@ControllerAdvice
public class GlobalNoSuchFlowDefinitionExceptionHandler {

    @ExceptionHandler(value = NoSuchFlowDefinitionException.class)
    public void handleNoSuchFlowDefinitionException(HttpServletRequest req, Exception e)  {
        // do whatever you need to do

    }
}

请注意,在正常情况下,只能存在一个异常解析程序。因此,在您配置LoggingExceptionResolver的情况下,将不会使用默认的异常解析程序来获取@ControllerAdvice等注释。

为了能够使用多个异常解析器,您需要配置HandlerExceptionResolverComposite

答案 1 :(得分:0)

这是另一种方法。 请参阅以下链接:

Spring Webflow 2 and bookmarkable URLs

注意:从SWF 2.3.2版本获取FlowHandlerAdapter的源代码。创建一个自定义适配器并使用NoSuchFlowDefinitionException的正确异常处理覆盖handle方法。

try {
                ServletExternalContext context = createServletExternalContext(
                        request, response);
                FlowExecutionResult result = this.getFlowExecutor().resumeExecution(
                        flowExecutionKey, context);
                handleFlowExecutionResult(result, context, request, response,
                        flowHandler);
            } catch(org.springframework.webflow.definition.registry.NoSuchFlowDefinitionException ex){
                // your logic
            }