My solution would involve one interface and one interceptor. You implement the following interface for all actions to which you are likely to want to redirect:
public interface TargetAware {
public String getTarget();
public void setTarget(String target);
}
The interceptor simply ensures that the target is set, if required:
public class SetTargetInterceptor extends MethodFilterInterceptor implements Interceptor {
public String doIntercept(ActionInvocation invocation) {
Object action = invocation.getAction();
HttpServletRequest request = (HttpServletRequest) invocation.getInvocationContext().get(StrutsStatics.HTTP_REQUEST);
if (action instanceof TargetAware) {
TargetAware targetAwareAction = (TargetAware) action;
if (targetAwareAction.getTarget() == null)
targetAwareAction.setTarget(getCurrentUri(request));
}
return invocation.invoke();
}
// I'm sure we can find a better implementation of this...
private static String getCurrentUri(HttpServletRequest request) {
String uri = request.getRequestURI();
String queryString = request.getQueryString();
if (queryString != null && !queryString.equals(""))
uri += "?" + queryString;
return uri;
}
public void init() { /* do nothing */ }
public void destroy() { /* do nothing */ }
}
From then on, once these two bits are in place and your actions implement the TargetAware
interface (if you expect to have to redirect to them), then you have access to a target
parameter in your JSPs whenever you need it. Pass that parameter on to your VisualizationAction
(which might as well implement also the TargetAware
interface!), and on SUCCESS
, redirect as explained by Vincent Ramdhanie:
<action name="Visual" class="it.___.web.actions.VisualizationAction">
<result type="redirect">
<param name="location">${target}</param>
<param name="parse">true</param>
</result>
</action>
I did not try every single detail of this strategy. In particular, beware of the notation surrounding the redirect
result type (depending on your specific version of Struts2: 2.0.x and 2.1.x may differ on this...).