Struts + MVC Philosophical question

Discussions

General J2EE: Struts + MVC Philosophical question

  1. HI!

    I'm using struts to develop an application. I have an action, that has an input page, and a result page. However, the input page needs information that is in the model. Following mvc, I canĀ“t access the model directly from view. With struts, the input page is called before calling the action. Strictly following MVC, how can I populate the input page with model info I need? Any ideas?
  2. In my previous project where we used Struts, for any page that required model info (in your case the input page) we would create a Action class for the page. This Action Class would takes a literally empty form.

    Any URL request (GET) to the input page would be routed through the Action Servlet in the same way as if we were posting the HTTP form to the result page. The action class would retrieve the model info required for the view and place it in the request object. The Servlet then of course does a forward to the view page to be displayed.

    Hope this helps,
    David

  3. Thanks,

    David,
    I'm thinking about what you said... However, I'm afraid that it will result in dozens of actions, what lead me to what "Race" said. Race, I agree about Struts being complex, but when we have complex problems to solve... That's the way to go. I working with a 3 developers team, and none of them had prior experience with J2EE. I tried to develop my own framework for view/model separation. However, it was taking too much time, and it was hard to explain the others what I was doing - ok, my fault. The time was short, so I decided to use struts, and give them some tutorials on it. The things are beginning to work, so unhapilly, I'm tied to struts for this one.

    I'm thinking about using tag libraries to access the model classes. What you thing about?
  4. I use custom html tags whenever possible. However, I don't extend TagSupport or any of the other official Tags. When I use a custom tag (I call them PageBuilders and they use the ElementConstructionSet from Apache) I simply import it at the top of the JSP...
    <% import com.PageBuilder %>

    I have no ".tld" defined. I think it is unecessary. (The best way to handle complex problems is to simplify) All of my custom tag methods take an HttpServletRequest as the parameter and return a String. So I can insert custom data into a JSP like so...

    <%=PageBuilder.getUserName(request)%>

    Or, build a complete table...

    <%=PageBuilder.buildUserListTable(request)%>

    All of the data required by the PageBuilder is either attached to the request or in the session. It's very simple. I have no reason to extend TagSupport. Which leads to my question, what advantage is there to extending TagSupport? Why bother learning a new syntax? Why should I bother with defining a ".tld" xml doc for my custom tags? It seems to me that this is just another excuse to use XML (the Holy Grail of modern computing)

    Am I missing something here?
  5. Hi,

    I do think calling Struts complex is a bit too much. I have never had any issues working with it and consider it one of the best frameworks out there for building a Web system.

    David
  6. Andre, I think Struts blurs the MVC. I think the ActionForm classes are a redundant, unnecessary layer. I prefer to place my model/domain objects in the session/request and also use them to directly populate my views. I would suggest bypassing the ActionForms and using your domain objects directly.

    Also, I don't like how the Action classes are modeled. One Action class per user action results in too many classes. I prefer using an Action class that handles a number of associated actions.

    IMHO, Struts is unnecessarily complex.
  7. <quote>
    Andre, I think Struts blurs the MVC. I think the ActionForm classes are a redundant, unnecessary layer. I prefer to place my model/domain objects in the session/request and also use them to directly populate my views. I would suggest bypassing the ActionForms and using your domain objects directly.
    </quote>

    I think that's a bit harsh.
    If you're pages are relatively simple and they retrieve data from relatively few objects, then accessing those objects directly is acceptable.
    However when you need to data from many separate objects it often makes sense to encapsulate that in a form bean.
    There is no silver bullet, use each technique where appropriate.
    Moreover, forms have other advantages in terms of validation, and passing any erroneous values back to the page.
    For instance, if I have a field on a form which is submitted, and then fails validation, it is nice to present the form back to the user in the same way it was originally submitted so the user can correct it.
    If you have no actionform, then you have to "roll your own" method of taking the parameters from the request, and feeding them back through to your page.
    Another great advantage of forms, is when your "form" actually spans multiple pages, for instance a long ordering process, if all the fields on the different pages are stored in a form, the form can be placed in the session, allowing the user to easily go backwards and forwards throughout the pages, even before any validation is done.
  8. Good points Tim. But I do the same thing with my Domain Objects. I am going to use the ActionForms. Maybe I will see the light after I've used them.
  9. Thanks for all of your answers. I decided to use Struts, and i'm going very well, thanks. However, I'm not using a strictly MVC aproach, since I'm not passing all requests through the actionservlet. I'm using jsps as frontends to user data. This jsps, when posting, call a struts action. When I need in the input jsp to use data from model to populate something(as a select box), I use custom tags. That's the design I choosed for this one.
  10. When makeing your service requests in struts have the urls call out to your actions directly with action.do. This allows you to perform any validation if needed using the action form and then perform your required model pings to get the data you need into the form or request and then mapping.findForward() to your JSP.

    You would follow this up by making access directly to JSP pages restricted using the standard mechanism in the web.xml for clamping down and a directory. You would probably need to allow the home or login page to have public access so you'll need to keep them seperated.

    The common design seen with Struts is indeed using an action class to describe each individual service. But you are not restricted to do this. You are able to use the same action for different requests by just forwarding to the same .do but with a flag that determines what type of request and therefore what processing needs to be done. The reason so many dessigns go with the first rather then the latter is to maintain a sense of modularity and to prevent "Magic" actions that approach the 1000+ lines of code.
  11. Another good reaon to have all your requests be action .do's is that everything goes through your front controller (if you extend ActionServlet) in which you can perform logging, authorisation checks, menu handling etc.