Hanen has posted 2 posts at DZone. View Full User Profile

Why Struts is Necessary for Development

09.16.2008
| 15671 views |
  • submit to reddit

I am thinking of a funny answer for this :) When someone feels hungry either he can cook and eat or go to a restaurant. Most people go to restaurant because it makes our job simpler: just eating. No need to prepare food and then wash the dishes. In the same way, with Struts web developer just has to write the business logic. Everything else will be handled by this framework. 

Struts is an open source framework which makes building web applications easier, based on Java Servlets and JSP technologies. The Struts framework was created by Craig R. McClanahan and was donated to the Apache software foundation in 2000. Struts is based on the MVC design paradigm; it is an implementation of JSP Model 2 architecture. The MVC pattern has 3 key components:

i) Model: responsible for business domain state knowledge
ii) View: responsible for presentation view of the business domain
iii) Controller: responsible for controlling the flow & state of user input

The Struts framework resides in the web-tier. Struts applications are hosted by a web container and can make use of services provided by the container, such as handling requests via HTTP and HTTPS protocols. This frees developers to focus on building applications that solve business problems.

Struts has mainly advantages only. You can say that in Struts you are using properties files for storing all variables, so that your application has no hard coding. This is the main functionality of Struts - let us suppose you are doing a project which is using the English language, but you want to enhance your project in another language such as  French or Chinese. Then there is no need to change the Action class or Bean class (no need to change anything in your complete project ). You just change your properties file.

Struts also follows a well defined order and every progammer has to follow that order. If you are a J2EE programmer, then I think you know that you are using one particular servlet as controller in which every request goes through, in the same way the Struts framework provides a way in which they implictly define all that controller functionality, you don't have to care about that.

Supposing that our web application has a large scope and acts with several JSPs and hence large volumes of requests/responses to/from server, the controller (Servlet) becomes very complicated. In that case we should move on to Struts. Struts has an in-built servlet (Action servlet) that takes care of these and controls the navigational flow, reducing the overhead and making job simpler.

Actually, Struts combines with JSP, Servlets, custom tag, message resource and action classes...so end result is synergistic platform, which makes developing web applicaitons easier and reduces the number of developers. It is easy to troubleshoot whenever error occurs and with its tag library we can easily create JSPs.

Published at DZone with permission of its author, Hanen Ben Rhouma.

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)

Comments

Bruce Fancher replied on Tue, 2008/09/16 - 4:06am

Struts is and always has been complete and utter garbage.  It may have some marginal utility above and beyond Servlets and JSP, but (to use a phrase that seems to be quite popular at the moment) that's just putting lipstick on a pig.  If you're doing serious web application development in Java, you should be using a component-based framework such as WebObjects, Tapestry or Wicket (and not JSF!).  Anything else very quickly becomes an unmanageable mess. 

But still, thanks for letting us all know about a widely-used, ten-year old framework, because otherwise we might not have heard of it.


Yo Tu replied on Tue, 2008/09/16 - 4:44am in response to: Bruce Fancher

Simply put, you're wrong.

There's much more than component-based frameworks. For a quick, content-oriented, straightforward website (eg, blogs, content managers, user oriented websites...), action frameworks are far better. Component based frameworks suit better as enterprise intranets, where component reuse is more common and URL mechanics (POST in JSF, strange url notations in wicket) are less important.

Anyway, it's true that Struts 1 is getting the "legacy" label nowadays. There are far better replacements:

Loom - http://loom.extrema-sistemas.org

Stripes - http://www.stripesframework.org

Juan Maya replied on Tue, 2008/09/16 - 4:39am

Wow...please tell me this article is 10 years old and has been just published or maybe finally the LHC created a hole in time-space and we are back to 1998?

 

John Denver replied on Tue, 2008/09/16 - 4:50am

More healthy for your self is throw out from the window Struts and If you really need to use an Action framework I suggest Spring MVC 2.5 or Stripes. Or even as the last commenter Tapestry, Wicket as component based framework or even JSF is much better than Struts. All this frameworks maintain the MVC pattern as you describe they learned from Struts mistakes and now those are much better and simple/easy to use.

Struts was good in its time but I think is done for new development,  Now it is good only for maintain legacy projects.

 One simple sample of Spring MVC 2.5 with freemarker:

Here is the controller and the view:

@Controller
public class Start {

@RequestMapping("/home")
protected ModelAndView index() {
String message = "Hello World";

ModelAndView mv = new ModelAndView();
mv.addObject("message", message);
return mv;
}
}
<html>
<head>
<title>HelloWorld with SpringMVC and Freemarker</title>
</head>
<body>
${message}
</body>
</html>

 Your configuration of web.xml witha servlet and filter for the spring context very easy just few xml lines as your applicationContext.xml file with the declaration of bean scan package and the Freemarker bean thas all. Really, really easy only 2 configuration files and Im talking that the web.xml max 10 lines the other is like 6 xml lines(For people that care about xml configuration).

 Dont get me started with Struts because Im really lazy now to post the Struts comparation.

Regards.

Danny Lee replied on Tue, 2008/09/16 - 5:17am

Struts is OK-Framework, a little bit outdated (it's better to use smth else for new projects) but not "bad" enough for migrating of existing projects.

 Still, the article is rather confusing and at least 4 years late :) 

 

 

John Denver replied on Tue, 2008/09/16 - 5:47am

Heck even Grails is an awesome!. It use Groovy as a language and use SpringMVC, Spring and Hibernate all well know frameworks or as I said you can use Spring MVC 2.5 it is really simple and easy to use.

Maybe as someone said I come to reliaze that this is a very old releated article or it is a joke?.

Regards.

James Sugrue replied on Tue, 2008/09/16 - 7:50am

Thanks for posting the article. Sure - Struts isn't the latest framework on the block, but it's always interesting to get developers opinions of various frameworks and libraries that are available.

James

Philippe Lhoste replied on Tue, 2008/09/16 - 8:02am

Most people go to restaurant because it makes our job simpler

I am not sure in which world you live, but in mine, most people rarely go to restaurant, most of the time only in special occasions.
I have probably a slightly above average pay in today's France standards (although I am obviously underpaid, of course!), but I certainly can't afford eating to restaurant everyday. Even less with my family.
It is a bit off topic, but your choice of metaphor is unfortunate, I fear... :-)

Danny Lee replied on Tue, 2008/09/16 - 8:51am

@Philippe Lhoste

 Too bad! I was in France a month ago a was eating in restaurants every day. Food was really great, tasty and "usual" food is even cheaper than here, in Germany. I can actually eat in a (not too fancy) restaurant 3 times a day, but it's the quickest way to get fat sick and ugly, because the food ist mostly too fat and not so healthy. But what I really like are the French supermarkets. You need no restaurants with such supermarkets! Outstanding. 

 So about struts... Forgett struts... use it if you have to, try something else if you can. Food is much more important ;)  

David Lee replied on Tue, 2008/09/16 - 9:05am in response to: Bruce Fancher

I couldn't agree more.  Struts and JSF are the 2 worst web frameworks I've ever been forced to use.

Frank Silbermann replied on Tue, 2008/09/16 - 9:24am

Yes, it's a good idea to separate business logic from the web presentation.  The problem with the Struts/JSP/STL/servlet approach is that JSP/STL/servlet is a crummy toolset for building the view.  (That it's better than CGI is the best one can say about it.)

That's not the fault of Struts, but Struts doesn't solve the problem, either.

There is a reason the world switched to object-oriented programming languages during the 1990s -- they're better.  Even though Java is object-oriented, the same cannot be said for tag libraries -- whether custom or standard.

Now that Wicket has some rigourous documentation (see the new Manning book _Wicket_in_Action_), you no longer have to be a pioneer to find something better.

John Denver replied on Tue, 2008/09/16 - 9:50am in response to: Frank Silbermann

I'm agree, Wicket its a great web framework, I'm still new to Wicket but I found it very interesting to do GUI compositions with the borders and panels. The model approach it use also good, The spring integration is awesome with annotations.

The only thing I dont like it is doesnt have Hibernate support out of the box, I have to use Spring all the time, I know there is Databainder but they even doesnt offer a simple jar, I have to use maven.

Also I hope for Wicket 1.4 they use wisely generics because if they use generics in all the framework even in the Component class I think is not a good idea, Wicket will get hard to use. But Im waiting for the Wicket 1.4 release.

Another good thing with Wicket is you dont need special tools for it just your Java IDE thats all it is just pure Java with HTML, I use intellij IDEA and it handle it very good. 

Really I recomend you Component Framework Wicket 1.3, Action Framework Spring MVC 2.5.

Hassan Chawiche replied on Tue, 2008/09/16 - 11:59am

This article gives an idea about the framework. JSF is a good replacement of it. We're using JSF since 2 years on large scale/projects, so far so good. Struts is a worst web framework ?! I might agree, but not on JSF.

Matthew Ryan replied on Tue, 2008/09/16 - 4:56pm

The bottom line is we live in a world where Struts 1 was a dominate web application framework and today is still in place anywhere and everywhere there's a Java web container. New people come to the Java web app world all the time so such an article has it's place. Thanks Hanen.

What is funny though are the really unimaginative people who bash Struts 1 in such a passionate almost emotional way. I'd say 'get a life'.

Struts improved on the straight use of Servlets/JSPs. Struts 2 (WebWork) and Spring MVC improved on Struts. And component frameworks like Tapestry, JSF, et al, are just another paradigm where there can be arguments for against. Those arguments link such an interwoven tapestry of issues it's almost ridulous to discuss them seriously over a couple paragraphs in a JavaLobby or Blog entry.

Maybe the types who are 'forced' to use Struts 1 have a right to rant because of the day to day pains they feel but at the same time, it would appear, they are forced to use Struts 1 because they lack the knowledge and skills to influence what they work with and where they work. Craig McClanahan put Struts together more than 8 years ago when we lived in a straight Servlet/JSP world.

Bashing Struts 1 in such a forum where people seldom list their grievances, even in bullet points, to me, seems to be such a waste of time.

What is of value are the comments mentioning other later and greater frameworks, their features and why people like them. If this JavaLobby entry introduces Struts 1 to the uninitiated and sparks discussion where developers contribute the real advantages and disadvantages of alternate frameworks then I'd say, "great post!"

Good job Hanen.

My two cents.

John Denver replied on Tue, 2008/09/16 - 7:28pm in response to: Matthew Ryan

@Matthew Discuss or bash frameworks in a technical forum is not a waste of time, I'll say it is good because that way we can see different opinions and what the people think and use.

It has been a good discussion so far until your reply really we dont need someone to elighment us, just it is that people are tired of Struts and I understand them, Struts is old and it is getting tired to write all that code it needs over and over again, why no better for new projects use a new Web Framework as Spring MVC or Wicket and you dont need to write all that xml code and actionforms etc?, as I said Struts still alive but for maintain a legacy stage.

Also this article is out of time this should be 4 years ago not now so the author should research little more before post this kind of articles that doesnt have sense at all.

Note: Even Im still thinking somone is joking with this article but anyway it is good for discuss differents web frameworks.

Matthew Ryan replied on Tue, 2008/09/16 - 8:07pm

My point is Bashing Struts without details, such as you've mentioned, is a waste of time.

Bashing Struts is what's called 'beating a dead horse'. If you haven't been 'around the block' with Struts then it's like 'where have you been'.

I'd say
- Bash Struts 1 without elaborating why = waste of time
- Bash Struts 1 with points = valid but who cares anymore
- Bash Struts 1 with points and give points which other frameworks are better alternatives = value
- Give points which other frameworks are better alternatives = even more value

Always good to thrash out the pros and cons of this framework or another but spending any time on pointing out the painfully obvious is .. tedious and over-done. If Struts came out 8 years ago and people have had issues with it since it's widespread adoption then we've been browsing over Struts 1 comments for nearly 8 years already.

I don't think about ex-girlfriends. I just move on. That's just me.

Matthew Ryan replied on Tue, 2008/09/16 - 8:09pm

"My point is Bashing Struts without details, such as you've mentioned, is a waste of time."

In case this sentence is misread - what I mean here is that bashing Struts without details is a waste of time, however I consider your bashing Struts with details valid.

Jeroen Wenting replied on Wed, 2008/09/17 - 3:11am

The only reason for anyone who's not severely masochistic to use Struts is to ensure lifetime employment for junior programmers to maintain the application.

That might be a good idea in fact as it will quickly weed out the weaklings and those among them who do not have the masochistic streak required to survive for long in this industry.

John Denver replied on Wed, 2008/09/17 - 3:55am

The transition from Struts to Spring MVC or Stripes is very easy, Think like this just drop ActionForms, every controller is a POJO and get rid of all the xml configuration instead use annotation in the case of Stripes or SpringMVC 2.5 or for Component based frameworks think the functionality for the presentation UI is encapsulate in a component or a POJO and declare it on the markup in the case of Tapestry or Wicket it is in Java code and you just use the HTML as structure. All this frameworks still using the MVC pattern why is hard for some people just move on?.

"Struts is to ensure lifetime employment for junior programmers" well someone have to maintain the legacy code already made with Struts, you can not throw away a project already working just like that, And if is really working why move it or port it?, Some apps can work more than 10 years without a modification just of course maintaining and get rid of bugs.

If your app is in Struts and is working great let the application like that is my suggestion but if you will start a new web app take a look to other frameworks I mention or also you could try the new RIA stuff as Flex or JavaFX.

Piero Sartini replied on Wed, 2008/09/17 - 7:05am

I wonder that there are so many people that fear the name Struts. It certainly was and is a very good and stable web framework. Personally I am using Struts 2 on some of my projects now... and it's really nice to work with.

Keep in mind that the web _is_ stateless. Action based frameworks fit in very nicely. Where is the point in putting a lot of complexity on top of it, just to have the same development model as on the desktop? You buy a lot of problems by doing so (reuseable urls, seo, memory consumption, etc).

Components are useful if you build desktop like applications... but since I am doing web development for many years, even there its much more natural to me to think in actions.

John Denver replied on Wed, 2008/09/17 - 11:56am in response to: Piero Sartini

@Piero Sartini By the way what Struts2 bring new?, I did a small sample one time in webwork but I guess it changed a lot since that isnt?, and I found that the docs are not very well but I saw there is a manning Struts2 book.

I personaly I like the direction of Spring MVC is going with the simplicty of Stripes but its still flexible and powerful framework as Struts1 but without the pain. For example, I can use Freemarker with Sitemesh is very nice instead of JSP or Tiles but I could use also those, POJO's, Convetion over Configuration, Configuration with XML or Annotations, Spring Webflow, Ajax/Comet via DWR it is cool and also integrates very well with the backend with Spring and Hibernate or JPA so I have a full stack out of the box.

Note: Piero is right in something in his comment, I love Tapestry and Wicket not so JSF but the competition is switching from Component based to Action based check the Microsoft folks with the ASP.Net MVC, They are loving that instead the traditional ASP.Net component based.

Regards.

Piero Sartini replied on Wed, 2008/09/17 - 12:29pm in response to: John Denver

@Sidewinder: I never used WebWork, since it is named S2 it has changed a lot as well (2.0.x to 2.1.x is a huge step). You are right: The documentation is not as good as it could be and it is hard to start. Especially because of the flexibility and the nature of its community. It took quite some time and much reading of the mailing lists to get an idea how to use S2 "the right way".

For example don't ever think about using the dojo-tags. Normally you end up using the "simple" theme and write your stuff by hand, or build your own theme. Maybe this is the most confusing "feature" of s2.. and it takes time to understand that its best to just ignore it ;-)

But to get back to your points.. everything you mentioned is possible with s2 as well: Freemarker, Velocity and JSP are the "standard" results... but writing another one is nothing hard. There was some discussion about a GXP result some time ago - if I remember correctly google gave their code for it. Of course it is also possible to use either SiteMesh or Tiles2.. Actions can be POJOs... they can even be written in Groovy or JRuby if you wish. Convention over Configuration is available, but you may also configure by annotation or xml if you like to. There is a DWR plugin, and you may choose your dependency injection framework. Struts2 can be used with Spring, Guice and some others. You may even write parts of your app with GWT or JSF - plugins are available.

 The next big thing is OSGi support.. so I think it will be possible to deploy parts of a s2 application at runtime. But that is nothing that works right now (there is however an experimental OSGi plugin...)

I did never use Spring MVC (because I don't like Spring ;-) .. but I think both are not that far away. Except maybe that Spring MVC is tightly coupled with Spring DI and cannot be used without it. For some people this is no problem or even an advantage... for me it is not ;-)

George Jiang replied on Thu, 2008/09/18 - 12:30am

Well, the devil in Struts 2 is ONGL. With Spring MVC 2.5, you can still use JSTL and JSP EL. I just don't want to de-learn and re-learn another tag library, but that's just me.

David Lee replied on Thu, 2008/09/18 - 9:03am in response to: George Jiang

Me neither and  if there is one single thing wrong with the entire java web framework madness is that everyone reinvents the wheel when it comes to the view.  I just did a grails project at work and Mr Rocher has reinvented just about everything.  Don't get me wrong, It's a nice framework, but why do i have to relearn how to configure log4j, learn yet another way to query the database in addition to SQL & HQL, relearn a new way to write a filter, learn another completely unnecessary way to write querystring and form parameters and last but not least a new tag library.  There is almost no knowledge transfer between most java frameworks. 

The servlet, jsp & jstl apis are pretty damn good.  Why not just learn them.  They are superior in flexibility and capability to most other web technologies (not all).  Yet the java world is the most fractured on this front. 

And then there is Spring. Beyond the DI styuff, Spring does nothing but wrap J2EE apis in their own.  Why not just learn how to deal with JMS, why not just learn the servlet API, why not just learn how to close a freakin hibernate session.  The hibernate template callback is one of the most unnecessary things I've ever seen.  My co-workers want to marry the Spring stack.  I'm thinking, you're writing a 2 tier web application using hibernate, write a serlvet filter to close the session and let's move on, but nooooooo.  We need to waste the day writing all of this ridiculous spring code.   

Wait...I'm a little off topic.  Struts sucks.  Go wicket, tapestry, and even grails !!

 

 

 

 

John Denver replied on Thu, 2008/09/18 - 11:38am

" Yet the java world is the most fractured on this front" Not only the Java but check the PHP with thousands of frameworks, Python hehe there are jokes that every sec someone made a web framework it was to easy to do it and Ruby well Rails is the popular but already there is 10 frameworks for Ruby heheh check this link http://accidentaltechnologist.com/ruby/10-alternative-ruby-web-frameworks/

If you want total control yes go straight to Servlets/JSP's but that is like the old times code straight to the metal assembly vs high level languages analogy but If you dont want to reinvent the wheel in every project better do a favor your self and use a framework as Wicket, Tapestry, Grails, Spring MVC, Struts2 or even Struts1 still ok.

Spring is the contrary of what you are saying Spring decouple your code from frameworks. You just write POJO's and you can decouple from the front end or the back end your code will last 50 years or more, it is the logic that stay almost forever, the UI change every year and the database well we dont know what is next but Im sure Java will last longer and your code will still valid for anything even you can reuse it for a mainframe system or anything you can think of and it is much better for testing and maintain. Spring is not big deal is just POJO's nothing more.

Regards.

David Lee replied on Thu, 2008/09/18 - 2:11pm in response to: John Denver

You missed my point, but that's ok. And I have to disagree with you having done plenty of ASP.net , php java and a little ruby.  Java web development is fractured to a point where you can know java, struts & sql but not be very effective at all in a spring, hibernate & spring mvc shop.

But the same goes for the good frameworks:  No overlap, little knowledge transfer. 

Spring is not contrary to what I'm saying. Spring is exactly what I said it was:  Remove the DI stuff and it's an api over the J2EE  api.   Spring is a big deal and is not just POJOs.  It's a massive framework.

Just because I think you need to know the serlvet & JSP APIs to be a good java web developer doesn't mean I'm advocating not using a good framework or writing apps using just servlets. 

Lastly, if Im hired to do a job I don't always have the luxury of choosing the framework.  So if I do grails one week and wicket the next, the only favor I do can myself by way of your suggestion is to not take the job.But since I do take the jobs and use the various frameworks I can easily say the only thing common among them all is the servlet api and for that reason, it's worth knowing and know well. 

And if you do know the servlet api very well you might think the way I do regarding many frameworks.... I could have gotten the same thing done, every bit as well, without using framework X.

 Once again, Struts does suck. Go wicket, tapestry and grails.

 

 

 

John Denver replied on Thu, 2008/09/18 - 9:23pm in response to: David Lee

I wrote this comment upstairs: "The transition from Struts to Spring MVC or Stripes is very easy, Think like this just drop ActionForms, every controller is a POJO and get rid of all the xml configuration instead use annotation in the case of Stripes or SpringMVC 2.5 or for Component based frameworks think the functionality for the presentation UI is encapsulate in a component or a POJO and declare it on the markup in the case of Tapestry or Wicket it is in Java code and you just use the HTML as structure. All this frameworks still using the MVC pattern why is hard for some people just move on?". You are right in something everybody need a good grasp of Servlets/JSP so they can understand very well how the frameworks works and they can deliver better code.

Also you as Java developer you have to be able to tackle any situtation, software framework, anything anytime. All this frameworks use patterns that ensure and follow the way how people program. So if Im doing right now spring with Tapestry and next come one job that they ask me I will use EJB3 with JSF it is not so difficult the transation plus some companies they put it more simple with training and that but if there is not training I will take a book on the subject read quickly and practice the concepts but almost all are the same thing. 

The problem maybe is when you move to another platform or language that can be little bit more of effort for example move from Java Struts to C# with Asp.net. Anyway if you are more confortable with the road path that the Microsoft folks presents you with the C# and Asp.net go ahead all this is your choice.

The thing about Spring you are talking about is for example the mail functionality the Spring folks reuse the API and they present you with an alternative for JEE but if you want you dont have to use nothing from Spring you just need the container and you ensure you are only using/doing POJO's and your code will be decopled from anything, you could move your code to JEE anytime and let me tell you, check the EJB3.1 spec and is practically Spring, There is an article that someone said with EJB3.1 the Spring vs EJB war will be over. Anyway my point is with Spring you dont code to a framework you just write lets call this Portable Plain Java Code for all the Situations.

Slim Ouertani replied on Tue, 2008/10/07 - 4:32pm

Hi,

I think that struts is dead.  Yes It was the pioneer for a long time but it's not a standard JEE framework. JSF is the predecessor : more simple, many components ..... Now wicket may be the best.

 

Do you think that lift can ba attached to the queue ?

 

Matt Campbell replied on Sun, 2009/11/08 - 11:53pm

Ahh, the glow of youth as it bounces off the fragile surface of innocence...

OK, Hanen, I will tell you that after 20+ years of IT work, struts is a non-starter.  The reason is twofold: MVC does not stand up to reality.  One cannot maintain MVC even within the three classes it needs defined.  It does not happen, and that is because it cannot.  By its nature it is conceptually vague and invariably flawed in implementation.  No one can separate logic from appearance, form from function, on the web.  They will claim they can to sell ignorant IT managers some new widget, but this is not a valid claim.  It is like saying they can make 4 + 4 = 12.  No go.

Second, Struts itself does not implement MVC correctly (that is because it can't).  Once that is gone there is only the argument in favor of productivity for it.  That argument doesn't hold because struts does not deliver on its promise of easy code reuse in the real world. It may in academe or IT meeting presentations where problems are pre-defined for success, but reality is all that counts.

10 years ago everyone wanted someone with "two years of struts/EJB experience", which made me laugh.  But these days, what few want ads that are out there want people with particular product knowledge working with particular packages.  This is because they learned the hard way that most times it is much easier to find a piece of configurable software that gets you 90% where you want to go at a cost of x in money and y in time rather than build it all (or try to) yourself at a cost of 10x in money and 5y in time.  Remember this and never forget: It's ALL about money.

But I have faith, young paduan, that time and experience will be your teacher, and you will need not the words of old Ben Kenobi to sway you. :)

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.