0

Let me clarify my intentions here.

I have developed a web application using JSF and Facelets. Now, my manager just told me I need to re-create the same application but in a RESTful way using Spring.

I have a few concerns that need to be addressed.

I know the Spring MVC and JSF MVC are different ideologies; where Spring is action based and JSF is component based. Meaning the only difference is that the component-based MVC is a lot more automated, saving you time, and diverting your focus to only the View and Model part.

I understand that JSF uses beans as well as Spring, and you can also do Dependency Injection/Inversion of Control in JSF, so I still do not see the advantages of Spring over JSF?

Can you create RESTful web application in JSF?

Why do I need Spring? What does Spring offer that JSF doesn't to create Restful web applications?

Viratan
  • 503
  • 3
  • 9
  • 19
  • 1
    It is JAX-RS in Java EE (introduced since Java EE 6) with Jersey, RESTeasy, Restlet etc implementations. Spring MVC is a story apart. "*So, I still do not see the advantages of Spring over JSF.*" Spring is a platform having several modules in it. JSF on the other hand is an MVC framework. Both are different things. – Tiny Jul 05 '15 at 20:34
  • 1
    Why do you keep comparing Spring to JSF? You should compare Spring to Java EE. – BalusC Jul 06 '15 at 06:08
  • Food for read: http://stackoverflow.com/q/29982657 and http://stackoverflow.com/q/18369356 – BalusC Jul 06 '15 at 06:14
  • @Balus, in your answer here: http://stackoverflow.com/questions/18744910/using-jsf-as-view-technology-of-spring-mvc You clearly state that Spring MVC and JSF MVC are full competitors, thus they can be compared – Viratan Jul 06 '15 at 13:37
  • "Spring" != "Spring MVC" – BalusC Jul 06 '15 at 13:38
  • What is the difference? – Viratan Jul 06 '15 at 13:50
  • Nvm I see, Spring is the entire framework and Spring MVC is just one component of it. – Viratan Jul 06 '15 at 13:52

2 Answers2

2

RestFul services and JSF works with 2 differents patterns:

  • spring MVC offers RestFul service functionalities. RestFul services are SOA (they provide data) and not "presentation-oriented". It means the data are send from the server to the client, and the client is in charge to render them. A common template is to implement the client with Html and Javascript to render the view in a web-browser (there are a lot of javascript framework which help in that task: AngularJS, BackboneJS, JQuery...). You may also consume these services from other client (java client, PHP, C#) and use these data in other services. The spring MVC API may also use the spring-web APIS to render views in HTML on server-side using template-engines like JSP, Freemarker, Velocity etc... In this case, the content send from server to client is not "data-oriented" but already rendered (it is generally rendered as an HTML stream, ready to be displayed by the client/browser)

  • JSF encapsulate this whole client-server-flow by getting data and render them to the client using JSF-templating and bean-management. This process is implemented by a 7 steps lifecycle. This lifecycle hide from the developpers the client-server communication process (but it internally use javascript/Mojarra on client side, and servlets on server side to implement this lifecycle). It is another way of managing client-server MVC pattern.

These are 2 differents "philosophies". An advantage of the RestFul architecture is that it is SOA: they are made to provide interoperability using standards data-format (Json, XML). Interoperability help you to change clients or server implementation without impacting one or the other (you may re-implement your client using another technology-stack in the future, without having to develop the server again. Or you may implement differents clients with different technologies : C#, .net, Java, Javascript etc)

With JSF, You can achieve the same goal implementing a format specific renderer. But I don't think it is the main use of JSF to be used as SOA.

About the dependency-injection used, the J2EE JSF framework will use the J2EE DI API (it is important to note that the J2EE implementation of DI framework required a J2EE-application-server to work), Spring is naturally based on the DI pattern and use its own implementation (which can run in a servlet-container).

The JSF implementation from spring-web, (see the spring documentation) use the spring core DI implementation. I think it offers the same possibilities as the J2EE implementation (excepting that it does not require an application-server, only a servlet container)

Francois Gergaud
  • 384
  • 2
  • 11
-1

If you want to do DI with JSF you need a Java EE container like WildFly, while Spring MVC is built on the Spring Framework and only requires a Servlet container like Tomcat. Spring's DI is more mature than Java EE's DI, Spring has been around since ~2003 while Java EE DI has only been around since 2010. So if your manager wants to use a Servlet container you won't be able to run your JSF application. Besides that, JSF is not designed for RESTful operations, you would want to use Jersey for that. While Spring MVC has it's own REST implementation (tho you can use Jersey with Spring as well).

For a short introductionary guide to building a RESTful web service with spring, check out https://spring.io/guides/gs/rest-service/

wggn
  • 77
  • 6
  • Yeah at this point I think I might just use Spring since it offers MVC, REST, and DI/IoC. Also, can I use facelets for the View part of Spring MVC? Meaning how will I create and represent my web pages? – Viratan Jul 05 '15 at 21:00
  • A common pattern is to write the front-end pages in HTML5/JavaScript which then communicate with the RESTful backend services. JS frameworks like JQuery or AngularJS make AJAX communication with RESTful JSON services easy. – wggn Jul 05 '15 at 21:22