Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
562 views
in Technique[技术] by (71.8m points)

java - Catch "dead" session in Spring webflow

I want to catch the exception thrown when I loose my session, not only because a session timeout (for reports for example). Also I want that this handler will handle only that specific exception and will not be a global exceptions handler or any thing like this. Basically, I want to catch exception org.springframework.web.HttpSessionRequiredException.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

With any of the proposed solutions below you should be able to handle the exceptions and perform logic on them.

Possible Solutions:

1. You can add a FlowExecutionListenerAdapter that will listen to all exceptions thrown and then you can filter by instanceof.

import org.springframework.webflow.execution.FlowExecutionListenerAdapter;
import org.springframework.binding.mapping.MappingResult;
import org.springframework.webflow.engine.FlowAttributeMappingException;
import org.springframework.webflow.execution.ActionExecutionException;
import org.springframework.webflow.execution.FlowExecutionException;
import org.springframework.webflow.execution.RequestContext;

public class LoggingExceptionFlowExecutionListenerAdapter extends FlowExecutionListenerAdapter {

    @Override
    public void exceptionThrown(RequestContext context, FlowExecutionException exception) {
         if (exception instanceof HttpSessionRequiredException) {
                // do something
         } else if(exception instanceof FlowAttributeMappingException) {
                // do something else 
         }
    }
}

and you need to add it in your webflow executor config:

<bean id="loggingExceptionFlowExecutionListenerAdapter" class="my.package.LoggingExceptionFlowExecutionListenerAdapter"/>

<webflow:flow-executor id="flowExecutor" flow-registry="flowRegistry">
    <webflow:flow-execution-listeners>
       <webflow:listener ref="loggingExceptionFlowExecutionListenerAdapter"        
    </webflow:flow-execution-listeners>
</webflow:flow-executor>

2. Another solution is to implement your own FlowExecutionExceptionHandler. Although it will be a global exception handler The method

public void handle(FlowExecutionException exception,
            RequestControlContext context) { } 

will allow you to have access to the context which will allow you to filter on many different variables. See also, How implement the interface FlowExecutionExceptionHandler

3. Another possible solution if you are using Spring Security project I believe Spring security will handle the exception for you automatically if you have the follow config:

<security:http auto-config="true" use-expressions="true">
        <!-- rest of config omitted -->
        <security:session-management invalid-session-url="/login"/>
</security:http>

and if you want to catch the exception and perform logic on it see the following answer: Logout/Session timeout catching with spring security

I would recommend the 3rd solution as it is the least intrusive. In the 3rd solution's link there are several really elegent solutions to handling sessions.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...