GeeCON Prague 2014*
The moose likes Struts and the fly likes Summary of Struts, by Struts newbie Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Frameworks » Struts
Bookmark "Summary of Struts, by Struts newbie" Watch "Summary of Struts, by Struts newbie" New topic
Author

Summary of Struts, by Struts newbie

Mikey Warren
Ranch Hand

Joined: Jun 15, 2001
Posts: 30
Hi,
I've just been learning the basics of Struts, and am thinking of suggesting its use where I work - we currently use a couple of inhouse 'frameworks' for templating HTML and loading/saving from database but I think Struts might help simplify our JSPs. Before suggesting it I thought I'd try summarising how it works to make sure I understood, and also try it out to see what potential problems might be. Since I'd gone to the effort of summarising it, I thought I'd post it here for any comments on what I've got wrong, and hopefully to help other newbies.
So heres my Quick Summary of Struts :
Struts is a framework for the development of web applications using JSPs.
A Servlet (class "org.apache.struts.action.ActionServlet") is configured in web.xml, with parameters specifying things like where the struts XML config file is. Also in web.xml this servlet is normally mapped to URL "/do/*".
JSPs are written using custom tags provided by Struts (obviously standard JSP code will work to, but the tatg help simplify the JSPs). Forms in JSPs map to Struts actions.
For each action (e.g. login, create user, submit score , etc. ) an Action class needs to be created extending "org.apache.struts.action.Action" and implementing the following function:

Also for each action a form class is created by extending ActionForm, this is a bean with getters and setters corresponding to the fields in the form on the JSP and a validate function which provides oportunity to check for missing fields, or format of data entered etc.
Signature of validate method:

In Struts 1.1 an alternative to creating a form class is to configure a dynamically generated form class, and also to employ client side javascript validation automatically generated from the config.
In the struts XML config file, forms and actions are configured.
The "Action" Servlet acts as the controller in the MVC pattern, the validate function of appropriate form class is called (if configured), if validation is OK then the approriate action object is used to handle the request via the execute method, otherwise the JSP with the form on is redisplayed with errors shown. The execute method interfaces with the "Model" - the main "business logic" of the application which probably interfaces with a relational database to do whatever it needs to do. Any errors encountered are added to an ActionErrors object and saved in the request, so they can be displayed.
Then an Action forward object that will be returned needs to be created based on the results of the action and config in the Struts Xml file - e.g. forward to display a list of users, or to display errors. The Struts framework then forwards the request to the appropriate URL based on the Action Forward object - normally a JSP, which acts as the "View" in the MVC, possibly displaying errors stored in the request.
This description has just covered the normal core functionality, Struts provides other stuff, and can also be configured to behave differently.
======================= End of summary
Problems With Struts
As I've only just started experimenting with Struts its a bit early to be sure of what the problems are, but anyway here goes:
  • Properties file will quickly get very big and unmanageable.
  • Language selection using properties file :- Ideally I'd like to make language selection dynamically by user, not by swapping properties files. Perhaps I'm missing something.
  • Yet another thing for developers to have to learn - not sure about this, its probably easier to learn Struts than work on a JSPs which submit to them selves with loads of Java scriptlets
  • Its too generic/complex/heavyweight - notice a few posts suggesting this, but again, not sure that I agree


  • OK - Thats it any comments welcome!
    [ March 25, 2004: Message edited by: Mikey Warren ]
    Junilu Lacar
    Bartender

    Joined: Feb 26, 2001
    Posts: 4474
        
        6

    Seems like you've got a good handle on it. Couple of comments: while I have seen struts urls mapped with "/do/*", I think "*.do" is more common (Ted Husted says this in his book "Struts in Action"). Also, regarding language support, I'm not sure what exactly you mean by "swapping out" properties files but you can provide as many properties files as languages you want to support. They can all exist at the same time. You or your user simply need to specify the locale to use and Java will take care of the rest.
    Generic/complex/heavyweight? Well, I'd much rather have the plumbing already done for me than do it all over myself and still come up with something very similar yet less robust and feature-ladden.


    Junilu - [How to Ask Questions] [How to Answer Questions]
    Mikey Warren
    Ranch Hand

    Joined: Jun 15, 2001
    Posts: 30
    Thanks for the reply.
    I was hoping that changing languages didn't mean just replacing the properties file with one for a different language - think I need to do a bit more reading up on that subject.
    Think I agree with you on the advantages of Struts, I was just interested in peoples opinions. It certainly seems preferable to what we've currently got which is JSPs with lots of Java code handling submits and presentation of data.
    Junilu Lacar
    Bartender

    Joined: Feb 26, 2001
    Posts: 4474
        
        6

    Originally posted by Mikey Warren:
    It certainly seems preferable to what we've currently got which is JSPs with lots of Java code handling submits and presentation of data.

    Ouch. Lots of scriptlets cause lots of headaches. Can't imagine anybody writing a non-trivial application that way and still keep whatever sanity they had in the first place...
     
    GeeCON Prague 2014
     
    subject: Summary of Struts, by Struts newbie