Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

struts as MVC

 
subodh gupta
Ranch Hand
Posts: 203
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi ranchers,

I want your help in understanding very novice concept MVC in struts what i understand is DispatcherServlet is Front Controller, jsp is view, FormBean is model and Action servlet is part of Controller.

PLease suggest me if am wrong some where.
 
Merrill Higginson
Ranch Hand
Posts: 4864
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're basically on the right track. What you're calling the "DispatcherServlet" is probably referring to the Struts ActionServlet class, and yes it is the main front conroller, and the JSP is the view. However, I wouldn't consider the ActionForm part of the model so much as a kind of "view helper", since it's tied so closely to the JSP.

The thing that's important to undertsand about Struts is that the framework itself contains no model components at all! The assumption is that you will write your own model classes using POJOs, EJBs, Spring, Hibernate, etc. following whatever patterns you deem most appropriate. You then just instantiate these model classes and call their methods from your Action classes, which you correctly identify as part of the controller.
 
subodh gupta
Ranch Hand
Posts: 203
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i actually thought form as a part of model because it is used render the response in the jsp which is the concept of model.

Rest all is clear thanks.
 
Brent Sterling
Ranch Hand
Posts: 948
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
First off..."Front Controller" is a different pattern and related but not part of MVC. It is a bit of a stretch to apply the original MVC pattern as it applied to GUI application to a web application, so you have to twist some of the definitions. The view that I have (and that seems to be the most widely accepted view) is:
- Model = Database and Business Layer
- View = ActionForms and JSPs
- Controller = Actions and struts-config.xml.

I also have to assume you are talking about Struts 1.x. Your ActionForms (FormBean) are very Struts specific and tied to your JSP. With Struts 2 it is easier to use your Model objects (DTOs or POJOs) directly, but I am not if that is a good idea (I have only played around some with WebWork/Struts 2).

- Brent
 
Merrill Higginson
Ranch Hand
Posts: 4864
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The main objection I have to treating an ActionForm as a model object is that each ActionForm must extend org.apache.struts.action.ActionForm. One of the main rules of developing a model is that it should be completely independent of which view or controller components will be used to manipulate it.

Once I have model objects extending Struts objects (which is primarly a view and controller framework) I've tied my model to Struts. That means that if later I decide to use a comletely different view for the same model such as Java Swing, for example, I now have unwanted dependencies in my model.

It's OK for an ActionForm to have references to model objects, but the ActionForm should not itself be treated as a model object.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic