Wednesday, July 29, 2009

Choosing a Java Web Framework

Choosing a Java Web framework for your project is not getting any easier. Here are some things I think about when choosing a Java Web framework.

  • Is it action based? Action based frameworks generally map actions (defined in an HTML form or URL) to code in the controller tier of the application. Action based frameworks are the most popular type of framework. Most developers have had at least some exposure to these kinds of frameworks. The most popular Java Web framework, Struts, is an action based framework. Examples: Struts, Struts2 (based on WebWork), Spring MVC, Stripes, Ruby on Rails , Grails
  • Is it component based? Component based frameworks create a component abstraction in the view tier. Event listeners capture user actions and map back to code in the controller tier. I am of the opinion that component based framework have a steeper learning curve than action based frameworks. Some will counter that component based frameworks offer greater productivity, once the framework is learned. Programming in a component based framework can sometimes feel like programming Java Swing components. Examples: JavaServer Faces (JSF) implementations (MyFaces, Tapestry, RIFE, Wicket, Google Web Toolkit (GWT), ThinWire, Echo2, IceFaces (JSF implementation), Click
  • Is it page based? Page based frameworks allow you to develop application made of many Web pages. To implement these Web pages, the developer creates HTML (using JSPs or templates), JavaScript and CSS files. All action based frameworks (that I know of) and some component based frameworks are page based. Most of these frameworks now have good support for AJAX. Because you write the Web tier yourself, you also have a lot of control and the ability to adhere to Web standards. Struts is a page based framework. Examples: Struts, Struts2/WebWork, Spring MVC, Stripes, Ruby on Rails (requires JRuby to run in Java), Grails, RIFE, JavaServer Faces (JSF) implementations (MyFaces), Tapestry, RIFE, Wicket
  • Is the Web tier generated? New AJAX frameworks such as Google Web Toolkit, require you to only code in Java. They then generate the entire Web tier for you, so you don’t have to code the HTML, JavaScript or CSS. You will still need to understand CSS concepts in order to style you components. This may be attractive you if you or your team doesn’t have much experience with JavaScript and CSS or you want to keep all of you application logic within Java code. These frameworks are component based and the style of programming is very similar to Swing. AJAX is used heavily by these framework to create a rich user interface. In traditional frameworks, there are many Web pages that work together to form an application. These frameworks may only have one page (a frame) where components and screens are swapped out using AJAX. Examples: Google Web Toolkit (GWT), ThinWire, Echo2, IceFaces (JSF implementation)
  • Is it JSP based? Many frameworks, including Struts, use JSP pages to generate HTML. JSP (JavaServer Pages) are the classic way to generate Web pages in Java. Tag libraries are used within JSP pages to generate HTML. Custom tags can be created fairly easily to add your own functionality. JSP pages get compiled into Java Servlets. Examples: Struts, Struts2/WebWork, Spring MVC, JavaServer Faces (JSF) implementations (MyFaces), Click
  • Is it template based? Some frameworks offer an alternative to JSP pages known as a template. Sometimes a framework only works with templates, while there are also lightweight template libraries, such as Velocity or FreeMarker, that can be used in combination with JSP frameworks. A template is often an HTML file with references to Java components made within the HTML code. Because these files are not JSP pages, they bypass the JSP API which many developers do not like to use. This also give some templates the ability to be viewed by a browser, which makes them nice for Web designers. I list some examples of frameworks that use template out of the box. However, as I mentioned earlier, JSP based frameworks can be made to work with templating technologies. Examples: Facelets (a view handler that can be used with any JSF implementation), Tapestry, RIFE, Wicket, Velocity, Clay (part of Shale), FreeMarker
  • Is it Rails like? The hugely popular Ruby on Rails (RoR) framework has defined its own class of Web framework. Without going into the framework itself, two fundamentals of this framework are “convention over configuration” and “don’t repeat yourself”. Rails like frameworks generate a lot of the application structure up front to make it easy to get up and running. Examples: Ruby on Rails (requires JRuby to run in Java), Grails, Trails
  • Does it support a dynamic scripting language? Many frameworks now have support for dynamic scripting frameworks. If you would rather do most of your Web programming in a scripting language rather than Java (there are many who would), then one of these frameworks might be for you. They tend to be action and page based and have good support for AJAX. Examples: Ruby on Rails (requires JRuby to run in Java), Grails (using Groovy scripting language)
  • Does it support IoC? You will often hear this referred to as “Spring integration”. The Spring Framework is a full stack J2EE application framework. There are many different sub-frameworks within the Spring Framework, and its Spring IoC (inversion of control or dependency injection) framework is its most widely used. IoC frameworks can make it easy to configure Java beans within your application, as well as provide a number of other benefits. Most of the popular frameworks have support in one way or another for Spring IoC. If this is important to you, then you should make sure the framework you are selecting makes it easy to work with Spring IoC. There are other IoC frameworks out there, such as HiveMind and Plexus, but Spring is currently the standard.
  • Does it support Web conversations? Supporting Web conversations is about tracking state across several pages, so if you are using a framework that is page based, then this may be important to you. If it is, then JBoss Seam and Spring Web Flow are frameworks to consider. They are not Web frameworks, but server side frameworks that integrate into the middle tier of your application. Seam uses JSF for its Web tier. Spring Web Flow can integrate with various Web frameworks. Shale, a framework for JSF, also includes support for conversations, as well as RIFE.
  • Is it testable? Many of the popular frameworks are building in formal support for mock objects and other testing concerns. Testing is critical and Web applications tend to be difficult to test.
  • How well does it support security? Is it well supported and maintained? Is it stable? Will it help me adhere to standards? These are things I think about on all projects when selecting a framework and they apply here as well. Especially here, since there are so many Java Web frameworks to choose from and new ones are showing up each month.


  1. Most Java action/page frameworks can use non-JSP views, like FreeMarker/Velocity/etc.

  2. Grails has GWT and Wicket plugins (which is awesome btw) so it does allow you to architecht your view in a component-based manner.

    Grails freemarker plugin and even gsps by themselfs are template engines.

    Grails is built in spring, so it does support IoC by default.

    Grails comes with spring-webflow by default but there one more plugin that I'm aware that has an awesome weblfow support: GrailsFlow.

    Grails instigates you to do tests and TDD, including support for webflows tests and such.

    Grails supports spring-security(formerly acegi) but JSecurity plugin is a pretty viable alternative and Starck security and several other security plugins.

    It matches all your criteria. Search over yet?

  3. Here's an idea... if you're building a company site that will possibly provide some sort of API for others to use, take a look at Jersey and using REST. I am replacing the use of Struts and Spring MVC with using Jersey to build a REST API that I can then call with AJAX calls for my own web UI, but also provide a nice REST based API for 3rd parties to use for their own purposes. This of course is useful for specific types of sites. I don't know that I'd do this for a content only type of site. But if there is a chance you may need to have your site accessed or interoperate with other sites in some manner, toss the MVC and go for REST. It's incredibly easy, you get the nice Java JAXB with json and xml support built in, can provide a very nice SDK and even a java client library using Jersey client that works directly with your API XSD... making it that much faster for others to start using your API as well.

    This isn't for everyone, but it does have some nice benefits when you need to build a site that will be used by others. Kill two birds with one easy stone and use Jersey with a REST architecture.

  4. This comment has been removed by the author.

  5. very nice summary, thanks. One thing to keep in mind though is that you don't need any framework other than standard JSP, JSTL, servlets, JDBC, and the EL (expression language) to make beautiful fast DB driven web sites. If you do a web site a week maybe one of these high powered frameworks is worth learning but if you do one a year don't bother. Also, be sure the browser back and forth buttons work and the pages can be searched by Google if it is an e-commerce web site. AJAX just doesn't work well for e-commerce sites.

  6. @charlie,

    Why does ajax not work well for e-commerce sites? Please elaborate on this.

  7. @kuukage... You forgot the fact that not everyone is willing to learn another scripting language... And imo if you want to use a scripting language to develop web applications faster use ruby instead of groovy and stick with rails...

  8. I think my article or others like it can be used to decide on Platform X, given you already know Platform Y. Of all the items I listed, I think the decision to go action based vs component based is one of the biggest and is why I listed those two items at the top of my list. If you have a team with Struts experience, then Struts2, Spring MVC, Stripes and Grails may all be good choices as they are all “action based”. However, there are so many factors in play, so I don’t think I would make my entire decision on that alone.