Using Spring MVC with JSF [duplicate] - spring

This question already has answers here:
Using JSF as view technology of Spring MVC
(3 answers)
Closed 7 years ago.
Im using Spring MVC by itself till now, works great, but i hear from friends that JSF is also good, has some good visual stuff, even though i dont really know much bout JSF. So my question is: Is it possible to mix Spring MVC with JSF? If it is, is it a good thing to do or theres a better way to do it?

Try to stick with this equations:
Spring MVC + ORM (hibernate) + JSP + Jquery
or
EJB + JSF + RichFaces

It is definitely possible but with lot of effort. It's better to use spring framework only for the DI for services and DAO layer and use JSF for front end components. You can define two controllers with two different mappings say one with jsf and one with do and forward requests accordingly.

JSF is primarily component based MVC framework and probably the first successful event based web technology (analogous to Microsoft ASP .Net).
One can easily integrate with Spring MVC and it is good approach assuming existing business/web-application has been developed on Spring framework.
It is better to go with JSF2+JDBC(or JPA2) on Tomcat7/JavaEE6 certified servers.
JSF+EJB3.1+EclipselinkJPA or Hibernate JPA/your choice of JPA implementation
JSF+JDBC with or without traditional DAO
(1)EJB3.1+JSF2.1/2.2+PrimfeFaces/Tomahawk/Icefaces. Primefaces is one of the best and most popular JSF implementation; because primefaces has got rich UI components and excellent support from forum and nice examples and documentation, inbuilt JQuery support. Few things would be better in Tomahawk adn Icefaces. With JSF2 you can use more than one JSF implementation (i.e. Primefaces & Icefaces). JSF2 is easily pluggable into JDBC, Spring, and other widely used java web technologies.
(2)Just use JDBC+JSF with or without traditional DAOs for small, medium and large commercial web-application projects; because your application would be more easy to develop and maintain, more portable across tomcat and other webcontainers, no need of JavaEE certified (J2EE app servers) servers.
JSF is MVC and best used as View part of MVC
Integrating Spring MVC and JSF 2.1

Related

Spring Boot with Jsf views [duplicate]

I am currently implementing a small Spring MVC PoC, and I would like to use JSF as the view technology since most people in my company are used to a J2EE with Primefaces environment.
Does Spring MVC 3 support JSF, or simply JSP? I have read multiple articles mixing the two.
My need is to create an appealing UI. Is there a simple way to do this using Spring MVC with the JSP as the view technology?
Our application uses schedules/calendars in multiples pages. It's basically a time management APP
You're making a conceptual mistake. JSF is not a view technology. JSF is a MVC framework. Exactly like as Spring MVC, albeit they have both a different ideology; JSF is component based MVC and Spring MVC is request based MVC. Thus they are full competitors. You cannot mix them. You should choose the one or the other. Instead, JSP and Facelets are true view technologies. Since Java EE 6 (December 2009), JSP is deprecated and replaced by Facelets (XHTML) as default view technology for JSF.
You can use Spring MVC with JSP view technology. You can also use Spring MVC with Facelets view technology (and many others). But you can not use Spring MVC with JSF components let alone with JSF component libraries like PrimeFaces. JSF output components may work, but JSF input components won't work at all. Spring MVC has already its own <form:xxx> tags for input. Even if you mix them, you will end up with half of the functionality from both frameworks in a mingled and confusing code base. This is not making any sense. If all you want is to use the same UI as PrimeFaces, just grab jQuery UI. It's also exactly what PrimeFaces is using under the covers. PrimeFaces is a jQuery-based JSF component library.
From the other side on, it can also be very good that you confused Spring IoC/DI with Spring MVC. Spring IoC/DI is in turn usable together with JSF. You can replace the JSF managed bean facility (#ManagedBean and friends) by Spring managed bean facility (#Component and friends), usually with the sole purpose in order to use #Autowired in a JSF backing bean. But that's it. The JSF MVC framework lifecycle, the JSF components and the view technology remain unchanged. The standard Java EE equivalent of that would be using CDI (and EJB).
The same story applies to Spring Security. You can use it together with JSF, you should however not follow Spring Security + Spring MVC targeted documentation/examples in order to configure it, but only Spring Security + JSF ones. Do note that Spring Security constraints on business actions only works when you replace the JSF managed bean facility by Spring managed bean facility. So that would still require a "Integrate Spring in JSF" as described in previous paragraph. The standard Java EE equivalent of this all would be using container managed security (JAAS/JASPIC) via <security-constraint> entries in web.xml.
The same story also applies to Spring WebFlow. You only also need to make sure that you're using most recent version of Spring WebFlow as older versions cause conflicts when used together with multiple JSF component libraries. Moreover, since JSF 2.2, new Faces Flows feature was introduced as part of standard Java EE API, hereby basically making Spring WebFlow superfluous.
Then there is Spring Boot. This does not have a direct equivalent in Java EE. Spring Boot basically enables you to execute a Java EE application using a plain Java application class with a main() method "in an easy and abstract way". Without Spring Boot it's surely possible (otherwise Spring Boot would never have existed), it's only a bit more work as to configuration as you have to take into account server-specific details based on its documentation. For example: Undertow and Jetty.
Coming back to JSF and Spring MVC, if really necessary, you can safely run Spring MVC and JSF next to each other in same web application, but they won't interoperate in server side. They will run completely independently. They will at most touch each other in the client side, if some JavaScript in a JSF-generated HTML page happens to invoke a Spring based REST web service call in the same web application. But that Spring web service would then not need/have to know anything about JSF in order to respond accordingly. The standard Java EE equivalent of that Spring REST webservice is JAX-RS.
The upcoming Java EE 8 will come with a new request based MVC framework, named just "MVC", based on lessons of both JSF and Spring MVC, hereby supplanting Spring MVC and providing a standard alternative to JSF.
See also:
What exactly is Java EE?
Difference between Request MVC and Component MVC
What are the main disadvantages of Java Server Faces 2.0?
What is the need of JSF, when UI can be achieved from CSS, HTML, JavaScript, jQuery?
When is it necessary or convenient to use Spring or EJB3 or all of them together?
Spring JSF integration: how to inject a Spring component/service in JSF managed bean?
Why Facelets is preferred over JSP as the view definition language from JSF2.0 onwards?
Spring MVC and JSF don't really mix. You can use JSF for the view related stuff and have Spring manage and wire the backed (services, daos etc.). But trying to match #Controllers with JSF pages isn't something that really works nicely (next to that both are different stacks request based against component-based).
To integrate Spring with JSF you will need to add the SpringBeanFacesELResolver to your faces-config.xml. That will lookup beans from Springs application context. For this to work you have to use plain JSF annotations and not the CDI based annotations.
Spring Webflow can be a help here. Check out this sample project.
https://github.com/spring-projects/spring-webflow-samples/tree/master/primefaces-showcase

JSF or JSF with SpringMVC [duplicate]

This question already has answers here:
Using JSF as view technology of Spring MVC
(3 answers)
Closed 6 years ago.
I am having a Project with J2EE that's not very big , i began using JSF and i found it very nice and easy , but by the time i was searching on the internet i found a integration called JSF and SpringMVC , so i am wondering is that a better way to develop application and is it recomanded for me to use it or i'll be doing fine with jsf all alone.
i saw this link
http://www.mkyong.com/jsf2/jsf-2-0-spring-integration-example/
Both JSF and SpringMVC are implementations of the MVC pattern, so using both framework together is not the best design approach. JSF is a component based framework but SpringMVC is a request based. However you can use both together to get benefit of the SpringSecuirty framework if you need security features in your applications. Another advantage from my point of view is you can use Spring IoC features without the need to use EJB and run your application in a normal web container like tomcat.
Finally, it's your decision based on your requirements.
That tutorial is talking about spring+JSF and not springMVC+JSF.
If you don't know anything about spring you should be doing fine JSF all alone.
There is no a better way nor a worse, they are just showing how you can integrate spring with jsf.

Using JSF as view technology of Spring MVC

I am currently implementing a small Spring MVC PoC, and I would like to use JSF as the view technology since most people in my company are used to a J2EE with Primefaces environment.
Does Spring MVC 3 support JSF, or simply JSP? I have read multiple articles mixing the two.
My need is to create an appealing UI. Is there a simple way to do this using Spring MVC with the JSP as the view technology?
Our application uses schedules/calendars in multiples pages. It's basically a time management APP
You're making a conceptual mistake. JSF is not a view technology. JSF is a MVC framework. Exactly like as Spring MVC, albeit they have both a different ideology; JSF is component based MVC and Spring MVC is request based MVC. Thus they are full competitors. You cannot mix them. You should choose the one or the other. Instead, JSP and Facelets are true view technologies. Since Java EE 6 (December 2009), JSP is deprecated and replaced by Facelets (XHTML) as default view technology for JSF.
You can use Spring MVC with JSP view technology. You can also use Spring MVC with Facelets view technology (and many others). But you can not use Spring MVC with JSF components let alone with JSF component libraries like PrimeFaces. JSF output components may work, but JSF input components won't work at all. Spring MVC has already its own <form:xxx> tags for input. Even if you mix them, you will end up with half of the functionality from both frameworks in a mingled and confusing code base. This is not making any sense. If all you want is to use the same UI as PrimeFaces, just grab jQuery UI. It's also exactly what PrimeFaces is using under the covers. PrimeFaces is a jQuery-based JSF component library.
From the other side on, it can also be very good that you confused Spring IoC/DI with Spring MVC. Spring IoC/DI is in turn usable together with JSF. You can replace the JSF managed bean facility (#ManagedBean and friends) by Spring managed bean facility (#Component and friends), usually with the sole purpose in order to use #Autowired in a JSF backing bean. But that's it. The JSF MVC framework lifecycle, the JSF components and the view technology remain unchanged. The standard Java EE equivalent of that would be using CDI (and EJB).
The same story applies to Spring Security. You can use it together with JSF, you should however not follow Spring Security + Spring MVC targeted documentation/examples in order to configure it, but only Spring Security + JSF ones. Do note that Spring Security constraints on business actions only works when you replace the JSF managed bean facility by Spring managed bean facility. So that would still require a "Integrate Spring in JSF" as described in previous paragraph. The standard Java EE equivalent of this all would be using container managed security (JAAS/JASPIC) via <security-constraint> entries in web.xml.
The same story also applies to Spring WebFlow. You only also need to make sure that you're using most recent version of Spring WebFlow as older versions cause conflicts when used together with multiple JSF component libraries. Moreover, since JSF 2.2, new Faces Flows feature was introduced as part of standard Java EE API, hereby basically making Spring WebFlow superfluous.
Then there is Spring Boot. This does not have a direct equivalent in Java EE. Spring Boot basically enables you to execute a Java EE application using a plain Java application class with a main() method "in an easy and abstract way". Without Spring Boot it's surely possible (otherwise Spring Boot would never have existed), it's only a bit more work as to configuration as you have to take into account server-specific details based on its documentation. For example: Undertow and Jetty.
Coming back to JSF and Spring MVC, if really necessary, you can safely run Spring MVC and JSF next to each other in same web application, but they won't interoperate in server side. They will run completely independently. They will at most touch each other in the client side, if some JavaScript in a JSF-generated HTML page happens to invoke a Spring based REST web service call in the same web application. But that Spring web service would then not need/have to know anything about JSF in order to respond accordingly. The standard Java EE equivalent of that Spring REST webservice is JAX-RS.
The upcoming Java EE 8 will come with a new request based MVC framework, named just "MVC", based on lessons of both JSF and Spring MVC, hereby supplanting Spring MVC and providing a standard alternative to JSF.
See also:
What exactly is Java EE?
Difference between Request MVC and Component MVC
What are the main disadvantages of Java Server Faces 2.0?
What is the need of JSF, when UI can be achieved from CSS, HTML, JavaScript, jQuery?
When is it necessary or convenient to use Spring or EJB3 or all of them together?
Spring JSF integration: how to inject a Spring component/service in JSF managed bean?
Why Facelets is preferred over JSP as the view definition language from JSF2.0 onwards?
Spring MVC and JSF don't really mix. You can use JSF for the view related stuff and have Spring manage and wire the backed (services, daos etc.). But trying to match #Controllers with JSF pages isn't something that really works nicely (next to that both are different stacks request based against component-based).
To integrate Spring with JSF you will need to add the SpringBeanFacesELResolver to your faces-config.xml. That will lookup beans from Springs application context. For this to work you have to use plain JSF annotations and not the CDI based annotations.
Spring Webflow can be a help here. Check out this sample project.
https://github.com/spring-projects/spring-webflow-samples/tree/master/primefaces-showcase

Why to use Spring not JSF (Advantages of spring over JSF)?

i don't know JSF very well, and i have a friend who is using only JSF
and he asked me a very open question, why do you use spring not jsf ?
and because of there are a lot of things about spring in my mind
i wasn't able to give some straight answers
so what are the advantages of spring over JSF ?
Spring and JSF are quite a different frameworks.
JSF is more of a web application framework and you can still use spring with JSF to manage the relationship and creations of the objects.
Spring is more of an entire j2ee application framework/plaform.
Spring MVC and web flow will help you to design web applications similar to JSF.
But spring with its IOC concept also brings a lot of other modules such as dependency injection, aop, DAO, ORM, integration, batch and social tools and much more.
You can also check the JSF-Spring combination - http://www.springsource.org/node/422
JSF is presentation layer, Spring business layer (roughly said), they can be used together and do not compete.
As said before, JSF is a request-driven, MVC web-framework, built around a light-weight IoC container and is designed to simplify and standardize the way UI-layer of a web-application is built.
Spring, on the other hand, is less concerned with UI-layer but its precise definition is hard to formulate. It can be said that the primary function of SF is to tie together different layers of a web application in a standardized way, at the same time abstracting away the implementation details of a particular web technology from the developer. As one of the consequences, the developer is freed from implementing "plumbing" and instead gets working module interconnections which are tested, implemented and used relatively easily. This should provide a boost to productivity and shortens development cycle.
This abstraction can be viewed through various Spring modules - Spring is heavily modularized, so you choose which components of the framework will you be using. Though it features a MVC framework (SpringMVC is mostly written as a reaction to the Jakarta Struts, whom the Spring developers deemed poorly written), Spring lacks a dedicated presentation module so you're free to use most of existing UI technologies (e.g. JSF, GWT-oids, etc.) - once you properly configure them in Spring.
In another words, the "scopes" of JSF and Spring are quite different (though not totally disjunct).

Experiences with integrating spring 3 mvc with GWT?

Given:
Spring 3.0 mvc has excellent REST support with one of the representation being JSON.
GWT simplifies development as UI is developed in java. But by default it uses RPC for client server interaction. But there is an option to use JSON.
Questions:
Can you share experiences with using Spring 3.0 mvc with GWT ?
What is the best approach to integrate these two frameworks?
Is the default GWT's MVP architecture only for client side and does it work well with JSON?
Thanks
Can you share experiences with using Spring 3.0 mvc with GWT ?
Yes. We've successfully built a whole large application around GWT and Spring MVC (1500 source files, 6 months in development).
Spring was the key to the project's success. Only with Spring we were able to test individually some pieces of the application on the server side.
What is the best approach to marry these two frameworks?
Ignore the default Servlet used by GWT and instead create your own Spring controller to handle incoming GWT-RPC requests. This blog post was the key to integrating the two techs.
We also successfully integrated other components: Flash for animated charts and third-party Javascript components for other stuff. These communicate with the server through JSON. So you have two or more kinds of URLs:
the *.rpc urls are for GWT components and are served by the Spring controller for gwt
the *.json urls are for other components and are served by another Spring controller.
Also, in our case, we shunned configuration with annotations and instead preferred configuration with the good old Spring XML files. They make it much more clear what's going on. Except for the #Required annotation; it's great to find spring beans that should be connected but aren't.
Is the default GWT's MVP architecture only for client side and does it work well with JSON?
GWT's MVP architecture works best if you follow the guide lines. Use GWT-RPC communication as Google suggests.
You can still have JSON for other client-side components.
Try this solution: GWT and Spring MVC Integration
It uses 3 classes. Its very simple, declarative and clear.
It's stupid to mix Spring MVC and GWT. Also it's stupid to mix Spring MVC and JSF... It's stupid to mix 2 MVC (MVP) frameworks together. But you can use Spring DI and GWT for sure!
You may want to check out Spring Roo. It will help you get started quickly with Spring MVC, especially when dealing with RESTful URLs. It also provides a means to automatically set up GWT "scaffolding" (GWT code to interact with the Spring MVC backend). Hope it helps!

Resources