Web2 with Java:Struts2, Spring MVC, Flex, JavaFX and Google Web Toolkit

Originally posted on the O’Reilly Books OnJava blog. 

My fellow Java Developers. Two years ago I wrote an article on ‘Web 2.0 and Enterprise Java – move over Struts‘ looking at what was likely to replace Struts 1 (then and now a de facto web standard). How did our predictions fare?

Remember that article (and this one) isn’t looking for technical best, but which is going to be a best investment of your time to learn (in a mercenary commercial sense). And if you’re deciding which to use in a project , which framework is going to be easiest to support in 5 or 10 years time?

Broadly speaking, the frameworks we talk about break into two types: those that treat the web as a set of pages, and those that treat the web as a set of components (think Visual Basic, Delphi or Oracle Forms act-a-likes).

So , what has changed in the last 2 years:

  1. The rise of Spring. Not only has it gone mainstream, but the Spring MVC, Spring Webflow and Spring-JavaServerFaces are very powerful and widely used web frameworks. A sign of how things have changed is that for Sruts 1 the Spring guys wrote the integration for the (then) bigger Struts framework. For Struts 2 , the integration was provided by the Struts community. With the forthcoming Spring 3 release the framework is increasing momentum; More annotations and less XML in Spring MVC; Rest Web Services out of the box, support for Dynamic languages like Groovy and Spring Webflow becoming a more ‘just use it where you need it’ solution.
  2. Adobe Flex and OpenLaszlo – Flash graphical interfaces on the Web, built using Java. I don’t think these will be *the* mainstream choice but I do think the will be more than a just a niche. And for design led companies, nothing else (not even Microsoft Silverlight) can come close in terms of a user ‘wow’ factor.
  3. JavaFX and Applets done right (Jim Weaver has a good article on this). More of a competitor to Adobe Flash as both are rich content in the browser using an easily obtainable plugin. JavaFX will appeal to developers because of it’s Java like syntax. I hope I’m wrong, but for rich web content, would you put your money on Sun (an Engineering led company) or Adobe (an almost apple-like design led one)?
  4. Frustration with JSF (Java Server Faces). For the last 3 years I’ve thought that ‘*this* is the year of JSF. I’m still waiting not because of lack of demand (as web apps become more complicated and use more Ajax they become more like the JSF component based model). It’s now uphill for JSF as I (and a lot of other Developers) have given up. I’m still waiting for the ‘EJB 3′ moment when JSF becomes more simple and more usable. Remember , we ‘re not talking about technically best, but which is going to be in widespread use.
  5. Google Web Toolkit (GWT). Looking at it one way , GWT is JSF done right – a component based web framework , but one that is fast and has a lot of community support. Even then it took me a long while to warm to GWT – I’ve bad memories of web-components that hide their internals (remember Microsoft Interdev 10 years ago?) . What got me over the hump was thinking of GWT as a compiler not to Assembly or bytecode , but to Javascript and HTML.

How has Struts 2 got on in the meantime? I’m not sure. Remember , Struts 2 is very different from Struts 1. Conceptually it’s very similar to Spring MVC (Simple Java Beans based with configuration); Slightly easier to learn and maybe slightly less powerful than Spring (although both are more than capable for most Enterprise web applications.

The ‘I’m not sure’ bit comes from two (non technical) factors:

  1. Struts 2 hasn’t achieved the massive Enterprise developer mind share that Struts 1 did. It’s a better framework, but it’s got more competition.
  2. If you’re using Spring in the middle tier, why not have one less framework and use Spring MVC (instead of Struts 2) in the presentation layer as well?

Back to the previous predictions , how did we get on?

Scenario 1: Adding Ajax to existing Struts Applications. Use AjaxAnywhere – closest to the approach taken in the article Sprinkle Some Ajax Magic into your Struts Web Application. Despite writing this article , I see the frameworks evolving rapidly to the point where you would only take such an approach for adding Ajax to ‘Legacy’ applications.

How did we do? I’d maybe widen the choice of Ajax Libraries (to include DWR , Dojo, Prototype and others) but the basic idea of evolving rather than replacing your Struts 1 app still holds true.

Scenario 2: Need Ajax Now for a new Java Application. Use Appfuse as it gives Struts, Ajax (with DWR) and the possiblity of JSF integration now, all ‘out of the box’.

How did we do? I still recommend AppFuse, as it combines (name-your-web-framework) with Spring Hibernate(and other ORM) and Maven. However I’d now tend towards choosing Spring MVC (unless you’ve a reason to use Spring 2), given that you’re probably already using Spring in the mid tier.

Scenario 3: Medium Term. Use an implementation of JSF (either MyFaces or whatever Appfuse promotes – probably Struts Shale). Struts Shale (JSF) has so far released only ‘overnight’ builds. Apache MyFaces (JSF) tool support and Ajax capabilities are likely to improve over time. Both Struts-Shale and MyFaces are likely to play well with AppFuse , making it a safe bet for investing your time checking it out.

How did we do? Struts2 and Spring both still give you migration route to JSF. But do you want it?

So out of the creative ajax-induced chaos of 2 years ago, I see 4 or 5 clear choices in Enterprise web frameworks: Struts 2 (as a follow on from Struts 1). Spring MVC, due to the huge mindshare Spring has on the mid-tier. Google Web Toolkit , both as a natural home of frustrated JSF developers , and because who’s going to argue with the people who gave us maps and mail? Flex, because Flash apps done well just look so good. And JavaFX, because Applets-haven’t-gone-away-you-know.

In my view, we would have been delighted to have any of these framworks 5 years ago. And each (for different reasons) is likely still to be popular in 5 years time. Your missions now is to pick the one that suits your project needs.

Moaning about Struts 1 won't help you move to Struts 2

(Struts is one of the most popular way for companies to build their websites. This was to be posted on Bill moaning about Struts 1 problems, but Bill’s blog isn’t accepting comments at the moment.)

Struts 2 Logo

Bill,

I hate to spoil your Struts 1 party , but most of these problems have been known for some time (and the Struts team would be the first to articulate them). Struts 2 is a huge improvement and , as you mention, there are good alternatives out there (including Spring MVC).

The problem is that migration from Struts 1 to (for example) Struts 2 , while easy, still carries a risk for the project in question. It can be hard to convince the business decision maker when all they see is pain (‘so you’re going to break the existing site?‘) for very little gain (‘where’s the immediate payback of upgrading?‘).

My advice is to stick with Struts 1 on existing projects. Use Struts 2 (or even better, Appfuse) on new projects. And for new code on existing projects, consider running them both side by side. They’re all tried and tested solutions.

Paul

Easy(ier) Ajax with Struts 2

Some time back , I wrote an article for Java.net about How to add Ajax to your Struts Application. It’s pretty straightfoward (as is Ajax underneath all the hype), but it still involves too much JavaScript for my liking. Nothing against JavaScript, but if you write it , you have to support it. Far better to use a standard Ajax library , like DWR , Prototype or Dojo.

(As a reality check , if you don’t know what Ajax is , this article explains what Ajax is and what it can do)
Struts 2 Logo

Along comes Struts 2 (. Be careful – it’s good , but very different from Struts 1 – it should have been called WebWork 3). It does a lot of things better – for example , the way it’s Actions are normal POJO’s makes it a lot of things easier to unit test.

Struts 2 also gives you Ajax ‘out of the box’. No writing of javascript, no debugging against various browsers; just configure and go. A full description of how to setup Struts 2 to use Ajax is in this wiki article. The quick version is:

  1. Setup Struts 2
  2. Add the Struts-Ajax URL and Div Tag.

That’s it. Ajax without the fuss.

Free Struts 2 Training (Outline)

Every company now has a web site. Struts is the most widely used Java framework for building these websites. Struts 2 is a radical overhaul, making it easier to use, yet more powerful at the same time.

In January, I will start delivering a course on Struts 2 for IACT – the Irish Academy of Computer Training.

Struts 2 Logo

This course is an overview to programming for the Web using Struts 2 and Java (free course outline here), including an introduction to the language for people already programming in other languages like Visual Basic. It covers what is different about developing web applications, the problems the Struts 2 framework solves, and how to develop applications within it using the Eclipse IDE. Optional components include JUnit, Ant, Log4J and building Web-Database applications.

If you’re a Struts 1 Developer looking for more than the free outline then I do plan to blog about upgrading your skills over the coming weeks. If that isn’t quick enough, you can always hire me for a 1-2-1 mentoring session 🙂

Update: The Course notes are now also available on the wiki / knowledgebase.