What is the need of JSF, when UI can be achieved with JavaScript libraries such as jQuery and AngularJS

JSF to plain JSP/Servlet/HTML/CSS/JS is like as jQuery to plain JS: do more with less code. To take PrimeFaces (jQuery + jQuery UI based) as an example, browse through its showcase to see complete code examples. BootsFaces (jQuery + Bootstrap UI based) has also a showcase with complete code examples. If you study those examples closely, then you’ll see that you basically need a simple Javabean class as model and a XHTML file as view.

Note that you should not see JSF as replacement of alone HTML/CSS/JS, you should also take the server side part into account (specifically: JSP/Servlet). JSF removes the need of all the boilerplate of gathering HTTP request parameters, converting/validating them, updating the model values, executing the right Java method to do the business stuff and generating the HTML/CSS/JS boilerplate code. With JSF you basically end up with a XHTML page as view definition and a Javabean class as model definition. This greatly speeds up development.

As with every component based web MVC framework, you have in JSF less fine-grained control over the rendered HTML/CSS/JS. Adding custom JS code isn’t that easy as you have to take the JSF view state in the server side into account as well (e.g. enabling a disabled button in JS side won’t enable the button in JSF side, which is in turn a huge security advantage). If that is however a major showstopper, then rather look for an action based web MVC framework like Spring MVC. You’ll only take into account that you have to write all that HTML/CSS/JS code (and prevention against XSS, CSRF and DOM-manipulation!) yourself. Also if you fall back from Facelets to JSP, you’ll miss advanced templating capabilities as well.

On the other hand, if you have a big JSP/Servlet/HTML/CSS/JS/jQuery based website and you’d like to refactor the repeated JSP/Servlet/HTML/CSS/JS/jQuery boilerplate code into reusable components, then one of the solutions would be JSF. Custom templates, tagfiles and components can aid in this. In that perspective, JSF stands above JSP/Servlet/HTML/CSS/JS/jQuery (and that’s also why it’s pretty important to understand those basics before diving into JSF).

You can find a real world kickoff JSF based project here: Java EE Kickoff App. You’ll see that it contains next to JSF as good HTML5, CSS3 and jQuery.

See also:

Leave a Comment