You know, Spring Web Flow could be the most aggravating library I have ever worked with. Well, it is at least in the top 5.

Riddle me this:

How do you retrieve the current request URI from within a currently running flow?  And how do you use that in your flow xml?

You see, Web Flow (rightly) swallows up the normal J2EE API that are available to you: request, response, etc.

So why in the world would you want to retrieve the request URI?? Well, flows can contain sub flows. And flows can be reused in different locations within your app. So what if you need to display some text or UI or links based on what part of your application the flow is running in? Well you can’t given the Web Flow API.

Supposedly, there is an External Context available to you in all flows that allows access to the original request and response. But there is no documentation surrounding it.  Getting dirty and firing up a debugger, I was able to examine craziness held within the External Context.  It turns out the parameters and attributes are stored in a RequestMap property of the context.  The standard servlet API request properties are stored here under the standard fancy names like javax.servlet.forward.WhatYouNeed.

Here is the solution I came up with:

Create an “Action” to use old Web Flow 1.0 parlance.  This action has a method: getLocation() that will handle looking up the value in the map:

public class HecticBoredom {
	 * Because WebFlow is the worst documented, worst edge case software
	 * EVER.
	 * @param map The ExternalContext.requestMap object
	 * @return The URI as a string
	public String getRequestURI(MutableAttributeMap map) {
		return map.get("javax.servlet.forward.servlet_path").toString();

Make sure you add a bean for this in your flows.xml:

<bean id="hecticBoredom" class=""/>

Now in your web flow definition, you can use the following to retrieve the Flow URI:

<evaluate expression="hecticBoredom.getRequestURI(externalContext.requestMap)" result="location" result-type="java.lang.String"/>

Note: you could probably skip the action class and do the map.get() within the OGNL of your flow (as long as you are using Web Flow 2.x). However, I didn’t have time to explore that for my needs.

About the Author:

Learned something? Great! Need help on your development project? I can help @ Brilliant Chemistry or get in touch direct:

  • Ruby on Rails
  • iOS Development
  • System Architecture & Performance

Get in touch:


No comments yet, be the first.

Leave a Comment