ChatGPT解决这个技术问题 Extra ChatGPT

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


B
BalusC

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?


In the past I have used JSF with Primefaces. I read a few articles mixing both. What I need is a quick and clean way to have an application with a nice UI. Would you opt for using facelets with jQuery or use a component library and move from Spring MVC to use JSF?
Sorry, I can't. I know absolutely nothing about you. Just pick the one you think/feel is the best fit for your own knowledge, the concrete functional requirements and the time space/deadlines, etc. If it were me, I would pick JSF and be ready in a week. For the very simple reason because I've never really used Spring + Spring MVC and it would take months to learn/grasp it properly. Your situation is of course different. You can't ask someone else a personal/subjective question which you can answer the best yourself. By the way, Stack Overflow is the wrong place to ask for opinions.
Indeed, just trying to obtain an expert advice. I have worked with JSF only in the past, and I think I will continue to use it in the future, and give up on Spring MVC. I just don't have the time to learn it, instead I will use JSF but my experience is also very limited, a couple of months and only one very small project.
@BalusC It takes whole day to read your whole answer with all links(Inception). But it clears all doubts. Many thanks!!
Wow, this post cleared lot of doubts with spring + JSF combo and lot others in the way! Thanks.
M
M. Deinum

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.


The idea that I had was based on this article itcuties.com/j2ee/jsf-2-spring-3-example-the-registration-app however the only reason that i would use JSF is because of it's components libraries like Primefaces, how can I do the same using pure Spring MVC?
Find some JavaScript libraries and create some client-side components for calendars. But as mentioned you can still use JSF on top of Spring, but you cannot have Spring MVC.
Indeed I was confusing the two concepts. Thanks for your reply!
M
Manoj Gupta

Spring Webflow can be a help here. Check out this sample project. https://github.com/spring-projects/spring-webflow-samples/tree/master/primefaces-showcase


You got the question backwards. Moreover, JSF 2.2 already offers @FlowScoped for the purpose.