File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes OO, Patterns, UML and Refactoring and the fly likes MVC + view.forward = loose coupling? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Engineering » OO, Patterns, UML and Refactoring
Bookmark "MVC + view.forward = loose coupling?" Watch "MVC + view.forward = loose coupling?" New topic
Author

MVC + view.forward = loose coupling?

John Peters
Greenhorn

Joined: May 25, 2007
Posts: 18
I believe this question is pretty simple, but has been stumping me.

If the MVC design pattern provides for loose coupling, why do you hard code the view page in the view.forward('thePage.jsp') of the model? Wouldn't this prevent you from sending the output to another type of view, which (under my possibly flawed understanding) is what makes MVC so loosely coupled?

Thanks!
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
why do you hard code the view page in the view.forward('thePage.jsp') of the model?


I'm pretty sure you wouldn't. The model should have no references to the view at all. Did you find that as an example somewhere?


A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
John Peters
Greenhorn

Joined: May 25, 2007
Posts: 18
Thanks for your reply, Stan.

Most of the examples I've been reading have hard coded the JSP view. Here is an example from the Head First: JSP & Servlet book (pg. 89).


In this example the "request.getRequestDispatcher("result.jsp") is hardcoded. I know this is a simple example, but normally isn't this done via a XML file? If not, how is it coded?
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
Ah, this is fine. The servlet acts as the controller part of MVC. BeerExpert is the model, and it has no knowledge of the view. Controllers orchestrate the view, so they often know all about the view. Some designs have a controller servlet per page so they can hard code things like nextpage.

It is possible to build much more generic servlet controllers. Many frameworks like Struts have exactly one servlet that uses some field on the request to look up the model and the next view. At a very high level it does something like:

Hope you're enjoying the Head First books. Let me know if all that made sense or jumped too far ahead.
John Peters
Greenhorn

Joined: May 25, 2007
Posts: 18
It does, Stan, thanks!
...and I just realized that I inadvertently put model when I should have put controller in my original post.. doh!


Many frameworks like Struts have exactly one servlet that uses some field on the request to look up the model and the next view.

Are you talking about the struts.xml file where you can have the app go to Foo.jsp with a SUCCESS result and Bar.jsp with a INPUT result? To me, that would make more sense for a loosely coupled architecture.
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
I have no hands-on with Struts, but I've used (and written) a handful of other Front Controller designs. The "centralized" variation takes all user requests through one servlet, which gives us a neat place to put management, logging, security, etc.

The XML file you mentioned is the configuration that tells the controller what to do. It gives you a pretty good hint what the controller does.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: MVC + view.forward = loose coupling?