HMVC: Chaining controllers & models made simple
Hallo everybody,
I registered that many people are looking for controller chaining or some kind of hmvc (hierarchical model view controllers) following this simple diagram:
Code:
View(Model1,2,3) <-> Controller 1(Model1) <-> Controller 2(Model 2) <-> Controller 3 (Model 3)
http://www.javaworld.com/javaworld/j...0721-hmvc1.gif
As seen at java.com
In this case you do not need to implement someting special in spring, you can just chain controllers and their execution if you define a following kind of controller within the parent and merge resulting model data:
Code:
public class Page {
/** Build hierarchical page flow by refererencing to other pages */
private HashMap<String, Page> subPages = new HashMap<String, Page>();
/**
* This is our page specific controller
*/
private Controller controller;
public Controller getController() {
return controller;
}
public void setController(Controller controller) {
this.controller = controller;
}
/** Here is our hierarchy !! */
public HashMap<String, Page> getSubPages() {
return subPages;
}
public void setSubPages(HashMap<String, Page> subPages) {
this.subPages = subPages;
}
public Page() {
}
}
Now you can simply define a ModelAndView Chain in the top page and any subsequent page by simply coding a model merger:
Code:
public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, Exception {
...
Map model = new HashMap();
model.put("selectedTopPage", selectedTopPage);
model.put("selectedSubPage", selectedSubPage);
model.put("selectedTopPageName", selectedTopPageName);
model.put("selectedSubPageName", selectedSubPageName);
...
/** THE FOLLOWING LINES ARE FOR CHAINING AND MERGING */
/** BOTH MODELS ARE MERGED, YOU COULD EVEN PUSH THE RESULTING VIEW FROM SUB PAGES*/
if (selectedSubPage.getController() != null){
model.putAll(
selectedSubPage.getController().handleRequest(request,
response).getModel());
}
return new ModelAndView(pageLayout.getView(), "model", model);
}
This works very perfect for me and should be applicable to most web applications page layout and navigation patterns with subsequent page structures and differnt controllers for each subpage. Please sorry me about not posting the whole code here, but it is not hard to implement. This approach even subsitutes the need for components or struts tiles :))
More detailed information about hmvc can be found here: http://www.javaworld.com/javaworld/j...21-hmvc_p.html
(MVC layers from Javaworld.com)
Just contact me when you need more information about it, I appreciate any direct questions within this forum or as openbc question.
Andreas Bednarz, Germany - Hannover
Senior J2EE Developer
https://www.openbc.com/hp/Andreas_Bednarz/
Also a well done approach
Hi Bob,
this is also a well done approach. I think it depends on your application structure and seems also to depend on your session behaviour. What I have tried to realize is a complete session independand approach, so that navigational elements do not have to be stored within user sessions and just depend on url patterns. My url pattern are like this:
Code:
{toppageaction}_{subpageaction}.html
and as example
Code:
index_mysettings.html
where index is bound to a pagelayout action and mysetting is bound to a subpage with an assosiated controller. If you visit my example application shown as viewlett below, you will see that my top level pages are located in the main selection, any click on them just changes to content only and that the view is fed by multiple models just as viewable in bottom text. Viewresolving behind this is done by velocity and #parse tag which includes subpages belonging to each toppage and subpage combination. Toppages layouts are not redundant but kept as a single velocity template. You could do it with jSP also the same way.
Main benefit of my way I that I can redesign behaviour and page layout within minutes without worry about my controllers just by shuffling my page layout beans.
http://www.viewletcentral.com/vc/vie...ml?id=41542646
My best,
Andreas Bednarz, Hannover / Germany
Chaining controllers - yet another implementation
Hi,
I have done something along the lines of Cowboy Bob's approach if anyone is interested. Code and configuration description are here in this blog:
http://sujitpal.blogspot.com/2006/02...in-chains.html
and some bug fixes to the original code:
http://sujitpal.blogspot.com/2006/02...in-chains.html
The code I have up here also allows you to chain controllers serially or parallelly, or in a hybrid chain.
This appears to be a fairly common requirement for apps which deliver portal-style pages, such as a Yahoo! finance page with company reports, stock charts, user's portfolio, etc, where the information is related for the user, but probably uses very different controllers for each component. Chaining independent controllers (which are easy to develop in parallel and unit test in isolation) to populate the ModelAndView appears to be better than writing a monolithic controller that calls on various services. Adding or removing a component in the former case is a matter of configuration change, while the monolithic approach means that there must be some code change.
So I am wondering why the Spring team does not include this sort of functionality in the distribution. I ask because I am concerned if there is some problems with this approach that I am overlooking, or if there is another better, recommended way to achieve this functionality.
Thanks
Sujit