Use jquery in jsf1.2 or just use jquery with jsp (jstl)
There are a lot of technical questions regarding JSF and jquery but this is a question about making a clean decision about these two widely used technologies. It's obvious that jquery and JSF 1.2 (Im always referring to JSF 开发者_如何学Go1.2) can be used together, but is it wise to use them together?
Or it would be just better to use a rich jquery-based frontend with pure JSP? Moreover, when using jsf with jquery would it be also necessary to use pure jsp as well? I hope that the experienced developers out there can give some very strong arguments while answering this critical question. Im really worried that if I start a jsf/jquery web app I will end up writting some hybrid weird code of jsf/jsp/jquery that would be mostly a trouble rather than a clean java web app.Thanks in advance.
ps: Im sorry if this seams to be not a pure programming question but I think it is more important to know the answer to this before finding all the small technical details.
jQuery can definitely be combinied with JSF. Even more, there exist component libraries which do that, such as RichFaces and PrimeFaces. Both have JSF 1.x targeted components with ajaxical capabilities and rich UI which are (partly) based on jQuery.
However, combining homebrewed jQuery functions in a JSF webapplication is indeed not exactly trivial. You have to take the server side state into account as well. E.g. when you show/hide a HTML element by pure jQuery, you have to reflect that as well in the JSF component tree on the server side to keep the both sides in sync. Otherwise the webapp may not behave as you'd expect.
When continuing with JSF, I'd strongly recommend to just head to an ajax based component library such as RichFaces, PrimeFaces or IceFaces. They have already taken all the nasty JSF-ajax integration details into account. Or when you really insist in homebrewing jQuery, then better head to a request based MVC framework instead of a component based MVC framework, such as Spring MVC. You only end up with more code.
See also:
- What is the need of JSF, when UI can be achieved from CSS, HTML, JavaScript, jQuery?
- Which Java web framework to choose for jQuery?
The interesting complication you will find with the JSF/JQuery combo is if you make any structural modifications to the page you can run into difficulty, i.e. adding form elements based on some client side event. Because JSF is a component based framework, it expects the model returned from the page to equal the model sent to the page.
JSF 2.x has provided considerably improved support for ajax and whatnot, but it can still make for 'interesting' coding sessions when you are trying to do rich client side modification.
Another consideration is the naming of components in JSF, which makes it difficult to select an element by id. You can use the $= to get an endsWith match in jquery, so you can work around it, but it still makes things more interesting than plain JSP.
JSF does get you some rich component libraries (primefaces, etc.) that may allow you to skip jquery altogether and still get a rich client feel.
精彩评论