Is there anything like this already in the Spring MVC web framework?
AFAIK Spring does not support this out of the box. I've coded a simple chained controler that does some of what you're requesting:
Code:
public class ChainedController extends AbstractController {
private Controller[] controllerChain;
private String defaultView;
/** Creates a new instance of ChainedController */
public ChainedController() {
}
public void setControllerChain(Controller[] controllerChain) {
this.controllerChain = controllerChain;
}
public void setDefaultView(String defaultView) {
this.defaultView = defaultView;
}
public String getDefaultView() {
return defaultView;
}
protected ModelAndView handleRequestInternal(HttpServletRequest request,
HttpServletResponse response) throws Exception {
boolean hasAlternativeViewName = false;
String viewName = getDefaultView();
Map chainedModel = new HashMap();
for (int i = 0; i < controllerChain.length; i++) {
ModelAndView modelAndView = controllerChain[i].handleRequest(
request, response);
if (modelAndView.getViewName() == null) {
throw new ServletException(
"Controller did not return a view name.");
}
if (!getDefaultView().equals(modelAndView.getViewName())
&& !viewName.equals(modelAndView.getViewName())) {
if (hasAlternativeViewName) {
throw new ServletException(
"More than 1 controller specified an alternative view.");
}
hasAlternativeViewName = true;
viewName = modelAndView.getViewName();
}
for (Iterator it = modelAndView.getModel().entrySet().iterator(); it
.hasNext();) {
Map.Entry e = (Map.Entry) it.next();
Object oldVal = chainedModel.put(e.getKey(), e.getValue());
if (oldVal != null && (!oldVal.equals(e.getValue()))) {
throw new ServletException(
"Different objects placed into the model under the same key ["
+ e.getKey() + "].");
}
}
}
return new ModelAndView(viewName, chainedModel);
}
}
HTH
Ollie