• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

MVC + view.forward = loose coupling?

 
John Peters
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
John Peters
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic