How can I access Spring Webflow FlowScope elements outside the flow?

user3120173 picture user3120173 · Mar 17, 2014 · Viewed 14.5k times · Source

EDIT: I didn't get any bites on this question, so I'm adding a little more detail.

I have a Spring Webflow app (ver 2.3.2). I need to access multiple FlowScope objects from inside the validation of one of the steps (not inside the flow itself). You would think this would be simple, but I haven't been able to crack it.

Spring Webflow provides a series of special EL variables which can be used for accessing the various scopes, but only inside the flow itself. Inside a custom Spring validator, there doesn't seem to be any way to get to them:

@Component
public class MyObjectValidator {

    @Autowired
    ApplicationContext context;

    public void validateMyObject(MyObject myObject, Errors errors) {

        FlowScope flowScope = context.someMagicFunction();  //  <---- UNKNOWN  
        MyOtherObject otherObject = flowScope.get("otherObject");  

        if (incrediblyComplexValidation(myObject, otherObject) {
            errors.rejectValue("myField","validation.fail","Your object failed validation.");
        }
    }
}

As you can see, inside a Spring Webflow Validator there is no direct external linkage to anything except the object you are supposed to validate. I need to get to those other objects in the flowScope. Ideally either through the ApplicationContext or some other environmental feature there must be a way to get to these other objects.

Anyone know the answer to this?

Answer

Prasad picture Prasad · Mar 18, 2014

You can get the scope beans from RequestContext - context holder for request-specific statecurrent web application context. Access to request context in your validator method is by:

    import org.springframework.webflow.execution.RequestContext;
    import org.springframework.webflow.execution.RequestContextHolder;

    RequestContext requestContext = RequestContextHolder.getRequestContext();
    requestContext.getFlowScope().get("objectYouAreLookingFor");