Spring & JSF integration: MVC Nuts and Bolts

I have attempted to integrate JSF with Spring MVC in the past, and whilst my first attempt worked, it was far from ideal. This time around I decided to take a few key decisions to help focus my efforts:

  • Drop backwards compatibility. There is just too much work involved with supporting JSF 1.2 and too much good stuff coming up in Spring 3.1 to ignore.
  • MVC annotations are king. @RequestMapping seems to be the preferred approach for most people; lets only support this and keep any custom annotations to a minimum.
  • Reduce dependencies. It’s nice to reuse stuff but this is an integration project so the less there is to integrate the better.

With this in mind I decided to take the org.springframework.faces.mvc.JsfView class from Web Flow as my inspiration. This class works really well because it only deals with the View in MVC, the Model and Controller remain entirely in the realm of Spring. The only problem with JsfView is the lack of postback support. We need to somehow detect the difference between the initial request for a view and any subsequent JSF postback.

Thanks to Spring MVC having a very flexible architecture this is entirely possible. We can have multiple HandlerMapping and HandlerAdapter beans registered with the DispatcherServlet. To support JSF we need something high up in this chain that can detect and deal with postbacks, leaving anything that is not a postback to be dealt with in the usual way. Here is the general sequence of events:

user               dispatcher    @controller
 |  /some/request      |              |
 |-------------------->|   maps to    |
 |                     |------------->|  creates
 |                     |              |------------> FacesView
 |                     |                             (/pages/file.xhtml)
 |                     |   render                        |
 |                     |-------------------------------->|
 |                     |                           [Delegate to JSF]
 |  response           |<--------------------------------|
 |<--------------------|
 |                     |
 |                     |
 | /some/request       |
 | (postback)          |
 |-------------------->|      postback handler
 |                     |--------->|
 |                     |    [Delegate to JSF]
 |  response           |<---------|
 |<--------------------|          |
 |                     |          |

The postback handler has a couple of interesting problems to deal with. 1) How do we know we are a postback. 2) How do we know what view to restore. Obviously a postback will be a HTTP POST operation, but we cannot blindly assume that all POSTs are JSF postbacks. We also need to know what XHTML file to restore, but this file is based on a decision taken by the @Controller of the last request.
The answer to both these problems is to write our own JSF ResponseStateManager. The ResponseStateManager is part of JSFs state management infrastructure and is responsible for reading and writing component state. Usually JSF will save the state data in the HTTP session and write a hidden form field within the page so it can be restored later. Hooking into this mechanism we can write an additional field for MVC, the presence of the field lets us know that we have a postback and furthermore the value will let us know what XHTML file to restore.

With the postback handler in place we now have the best of both the Spring and JSF worlds. We can use @RequestMapping annotations to build expressive URLs and JSF components to render complex web pages. If want to we can even return different Views for the same URL based on entirely different technologies (for example by inspecting the HTTP header we might decide to return a JSF page or a XML document).

If you want to look at postback handler code it is available here. The usual caveats of this being a moving codebase apply.

Reference: Integrating Spring & JavaServer Faces : MVC Nuts and Bolts from our JCG partner Phillip Webb at the Phil Webb’s Blog.

Related Whitepaper:

Functional Programming in Java: Harnessing the Power of Java 8 Lambda Expressions

Get ready to program in a whole new way!

Functional Programming in Java will help you quickly get on top of the new, essential Java 8 language features and the functional style that will change and improve your code. This short, targeted book will help you make the paradigm shift from the old imperative way to a less error-prone, more elegant, and concise coding style that’s also a breeze to parallelize. You’ll explore the syntax and semantics of lambda expressions, method and constructor references, and functional interfaces. You’ll design and write applications better using the new standards in Java 8 and the JDK.

Get it Now!  

One Response to "Spring & JSF integration: MVC Nuts and Bolts"

  1. Michael Angelo says:

    Are there any examples on how to use this? I really need to be able to integrate Spring 3 MVC with JSF 2.

Leave a Reply


seven − 1 =



Java Code Geeks and all content copyright © 2010-2014, Exelixis Media Ltd | Terms of Use | Privacy Policy
All trademarks and registered trademarks appearing on Java Code Geeks are the property of their respective owners.
Java is a trademark or registered trademark of Oracle Corporation in the United States and other countries.
Java Code Geeks is not connected to Oracle Corporation and is not sponsored by Oracle Corporation.

Sign up for our Newsletter

20,709 insiders are already enjoying weekly updates and complimentary whitepapers! Join them now to gain exclusive access to the latest news in the Java world, as well as insights about Android, Scala, Groovy and other related technologies.

As an extra bonus, by joining you will get our brand new e-books, published by Java Code Geeks and their JCG partners for your reading pleasure! Enter your info and stay on top of things,

  • Fresh trends
  • Cases and examples
  • Research and insights
  • Two complimentary e-books