Manage beans via Spring or via JSF - spring

In my JSF Webapp I am using Spring vor DI etc.
At the moment I have my beans managed by Spring.
(SpringBeanFacesELResolver defined in faces-config.xml)
But I'm not sure, if this is the right way...
For example: A bean defined in Spring cannot use the new JSF 2 view scope, right?
My question is not, if I should use Spring or not... Just HOW to manage my beans?!

You should in any case stick with one DI in your project. For me, Spring is clearly much more powerful that JSF managed beans. So I would opt to Spring in this situation.
You can hanlde view scope in Spring using custom scopes, it is quite straigtforward actually. Here's a blog post on this question.

Related

Using Spring with JSF/Facelets [duplicate]

This question already has answers here:
Using JSF as view technology of Spring MVC
(3 answers)
Closed 7 years ago.
I would like some clarification on some things.
I have developed a web application in the Eclipse IDE using JSF and facelets (xhtml web pages). Now, my manager told me to re-create this application but as a RESTful web application using Spring.
I did some research on Spring MVC and its DI/IoC features and have a few questions:
I see that Spring offers DI/IoC, does JSF not offer that?
Can you create RESTful web apps in JSF?
Since JSF uses beans just like Spring, what advantage is there to use Spring over JSF?
Are the facelets just the view part of the Spring framework?
Can I use Spring IoC/DI with facelets?
I am starting to think he just wants me to keep the project as is but add the IoC/DI of Spring.
As you can see, I am quite confused on this subject and would like some clarification.
I can understand your confusion. Once I did a project in which I used JSF as front end and Spring MVC as backend.
for you the answer is Yes , You can do inversion of control using JSF by using JSF Beans (for instance managed beans) but you need to keep in mind couple of things.
1: You need to register beans both for JSF and Spring.
2: Its recommneded that you should set same type of scope for this bean in JSF and Spring.
for example: if you are setting request scope for a specific bean in JSF then you should also set Request scope for the same bean in Spring. Only view scope will not be available in Spring but you can write custom view scope which is not big deal
Answer for the part of question Are the facelets just the view part of the Spring framework?
No, you can't say it is part of spring, facelets are handled by JSF but yes , they mostly used as a view part. So when you have a plan to make a application with JSF and Spring then I will interpret it as you are using JSF for front end and Spring for backend.
Restful services using JSF:
Yes, you can make restful service using JSF. here it is very good blog regarding thisRestful web Services in JSF You can also create restful web service in Spring Rest Web Services in Spring

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

Migrating application from Spring to CDI

I would like to migrate an application from Spring 3 to Weld. In order not to rewrite the whole bean configuration at once, it would be cool if I could inject part of the application that is still written in Spring in the new CDI part.
Is that possible?
This should be possible. CDI has an extension system where you can hook into the injection mechanism. From there you can manually bootstrap your Spring beans and then return those.
I wouldn't be surprised if there's already an extension for this, so before looking into writing one yourself check if there's not one already.
There have been a few attempts at making CDI and Spring work together. Take a look at CDI Advocate for one.
It comes down to how much of your app is spring. If you're running something like Spring MVC, it'll be very difficult since it manages essentially your whole app.

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

Spring iceFaces integration questions

i intend to use IceFaces with spring framework
and i have some questions, before going to this process:
Is it possible to use it with spring-mvc ?
If it's possible to use it with spring-mvc, what's better mvc or webflow ?
Can i use spring controllers as beans instead of the JSF managed beans ?
How the binding process is done, through spring binding or another way ?
please advise, if there are suggested links it will be great, thanks in advance.
You can mix spring with JSF easily for bean management. In such scenario spring can entirely replace JSF managed beans support and you will be able to address spring beans directly from your EL expressions in xhtml pages. The mechanism is described in spring documentation: http://static.springsource.org/spring/docs/3.1.0.M2/spring-framework-reference/html/web-integration.html#jsf
I don't think adding spring-mvc or spring-webflow to this mix makes much sense. JSF relies on its own servlet. You may use spring servlets for some side tasks like generating documents or specific views which can't be rendered by JSF but I'm not sure your project will really need that. For basic usage JSF is exclusive with spring-mvc/spring-webflow.

Resources