Posts tagged JSF
The Power of Facelets (Part 1 – Motivation)
Dec 15th
Ever since Java Server Faces (JSF) were released, we utilized them successfully for many web application projects. While being conceptually really nice and well thought through, one problem with JSF has always been the mismatch with JSP (Java Server Pages).
The issue is how to integrate JSP’s dynamic content into JSF’s component-based model. JSPs are singularly focused on generating dynamic output, whereas JSF requires JSP to coordinate building a component model. The disjunct occurs because that task is beyond the original intention of JSP.
Even though most JSF developers simply learn to navigate such problems, it made the usage of JSF in large projects a huge pain and always resulted in ugly code that was difficult to read and maintain. Far from what you want to achieve with a new technology.
The Problem with JSPs
…Well – maybe not JSP per se. JSPs are great for their intended use: Mixing both static and dynamic content made available by other parts of the application – servlets, plain java code, databases, etc.
But keep in mind that – historically – JSPs are nothing more than “fancy servlets” with some added semantic sugar. Their sole purpose of existence is to generate a response to a request; a JSP page is processed in one pass from top to bottom, with JSP action elements processed – here it comes – in the order in which they appear in the page.
JSF are component based and have a much more complex life cycle. They are created, asked to process their input (if any), and then asked to render themselves (I am simplifying here). For JSF to work well, these three things must happen separately in a well-defined order, but when JSF is used with JSP, they don’t. Instead, the component creation and rendering happens in parallel, causing all kinds of problems.
The fact that both JSP and JSF components add content to the response is another cause for a lot hard to track down issues. Unless you understand the difference between how these two technologies write to the response, you’re in for a lot of surprises – such as content appearing out of order or not at all.
There are severe limitations on how JSF and non-JSF tag libraries can be mixed in a JSP page. On top of that, developing a JSF application requires understanding the event-driven component model.
When learning JSF, a JSP developer now has to not only understand those (likely unfamiliar) concepts, but also learn the pitfalls of how to use the two technologies together and – more importantly – how not to. It’s a lot to learn and often unintuitive.
Lets have a look at a very simple example that illustrates the issue with parallel component creation and rendering. Say you want to create a label for a checkbox – bread and butter which we developers don’t even want to think about, right? Intuitively you would write something like this:
There is no intuitive reason this shouldn’t work, but if run this example, you’ll notice that no <label> element is rendered the first time you request the page, but if you request the page a second time, a <label> element is rendered.
This strange behavior has to do with the combination of JSF and JSP. When JSF receives the first request for the page, the component tree for the view represented by the page doesn’t yet exist. JSF creates a tree with just a UIViewRoot at the top and forwards the request to the JSP page to add the real components.
The JSP container now processes the page and invokes the JSF action tag handlers (here it comes again:) in the order they are encountered. A JSF tag handler looks for the JSF component it represents in the component tree. If it can’t find the component, it creates it and adds it to the component tree. It then asks the component to render itself. This means that on the first request, the components are created and rendered in parallel. On subsequent requests, the component tree exists, so no new components are created; the tag handlers just ask the existing components to render themselves.
On the first request, the >h:outputLabel< action creates its component and asks it to render itself. To do so, it needs to find the component identified by the for attribute, but this component doesn’t exist because the action element that creates it appears after the <h:outputLabel> element and hasn’t been invoked yet. Hence, the component created by <h:outputLabel> action can’t render its <label> element. On the second request, all components exist, so the component represented by the <h:outputLabel> finds the component the label belongs to and happily renders the label element.
Of course in this case there are easy workarounds, but hey: Why should I “work around’ something as simple as adding a label to a checkbox? Furthermore – as you start going beyond “Hello World” pages, you encounter more and more examples like the one above, and many of them aren’t as easy to understand. Matters get worse once you start using AJAX, A4JSF and similar technologies.
In a recent project, the code for the view pages were – out of necessity – plastered with <f:verbatim>pages – creating pages with unbalanced tags (hey – we had no choice) and making it almost impossible to work with.
The solution was – of course – to dump JSP altogether and move to Facelets – more about this in the next article.
J2EE – Java Server Faces (JSF) and EJB3 – for Legacy Modernization
Dec 15th
We just finished a project with the objective to rewrite and modernize a legacy web application using JSF and EJB3. The new application was to provide a rich, interactive map component as well as a “desktop-like” user experience with interactive tables, split as well as tabbed panes, etc. Let’s have a quick look at the two main tools used for this project:
Enterprise Java Beans (EJB) 3
It’s always a headache if you don’t have control over your data model. Especially when the model is bad. Especially when the database model is really bad – carried over from the old mainframe days and grown (note the avoidance of the word “matured”) ever since. EJB3 handled this job extremely well. Only 4% of project time was spent on backend implementation, a fair share of that on “Mock Data Services” for testing purposes. Nice…
Java Server Faces
Introduced as an event-driven component model for web application development, Java Server Faces are similar in spirit and function to the models used for standalone GUI applications (like Swing).
JSF shows the signs of a typical “Sun-Version-1″: Nice, well thought through concepts, combined with a very basic, almost rudimentary component set, forcing you to either use third party libraries (often times impossible in large corporate environments due to policies, etc) or “reinvent the wheel” and build your own component sets.
I like working with JSF for many reasons, but once things move beyond “Hello World” and “Guess-The-Number”, you have to know the internals of JSF very well or you will spend many hours debugging issues that are far from being intuitive.
Don’t get me wrong, I am enthusiastic about JSF and think it’s one of the better frameworks out there for large scale web applications, yet I am very much looking forward to version 2….
Some of the things I like about JSF:
The concept of “Component Renderer”, common to desktop frameworks, allows for easy and elegant combination of JSF with existing WEB 2.0 technologies like AJAX.
Being able to reuse existing component renderer for custom components makes it very easy to create new, complex and reusable components for complex and rich GUI elements.
Security: JSF fits very nicely with security libraries like JAAS. Used properly (custom component renderer and phaselistener), the front end developer can be shielded from any security related code without compromising security. I still love the little things like our “securityOutputLink”, which renders a simple text string if the user does NOT have access to the link target, and a regular link otherwise. In the backend, the decision is based on the same xml file which links user roles to page access, all with a handful of code lines. Sweet!
Configuration of navigation rules and backing bean properties in faces-config.xml is a very nice concept and allows “out of the box” for different application configurations for development, testing and production, which is a real timesaver.
On the Minus Side
The very bad integration of JSF with JSP and JSTL. This is certainly my number one criticism when it comes to JSF. As someone put it so nicely: Trying to combine JSF and JSP is like trying to shoehorn a foot into a glove: it’s possible, but it’s really just a stopgap measure until something better comes along. It would be nice, real nice to have a powerful expression language at your disposal. Sometimes you just want a loop without having to write your own component renderer, or conditional execution on a page without having to worry and work around the different variable scopes and execution times of JSF and JSP. Read here for more details
Small and very basic component set. Sortable, pagable tables with row highlighting and selectable rows and columns are certainly bread and butter for every real web application. Since the number one reason for using frameworks like JSF is to make a developer’s life easier, it’s hard to explain why I have to still write this myself (or switch implementations).