Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
The moose likes JSF and the fly likes Please help - More specific question on JSF + jQuery Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSF
Bookmark "Please help - More specific question on JSF + jQuery" Watch "Please help - More specific question on JSF + jQuery" New topic
Author

Please help - More specific question on JSF + jQuery

Melinda Savoy
Ranch Hand

Joined: Jun 21, 2005
Posts: 386

Just a quick question if someone in this forum has worked with jQuery and JSF. Unfortunately not many people in the jQuery forums have worked with JSF and therefore I'm having major problems trying to get help on my question below.

When jQuery executes an ajax call to a specific URL where in that JSP page a specific bean is being called and jQuery is passing in the parameter and in my IDE I have a BREAKPOINT setup in that SET method and if that method is actually being called won't the processing stop at my breakpoint? And if it is NOT stopping then can I safely assume that I'm never getting to my bean?

Here is my jQuery code:



Here is the changeSelectedFacilityCode.jsp page:



If anyone can please take a look at this code and let me know what I'm doing wrong it would be appreciated. The generated SPAN from jQUERY is never getting populated and therefore I cannot see it in my alert .
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 15957
    
  19

Well, technically, you can never "call" a bean - JSF or not. A bean is an object, so you can call a method defined on that object, but you can't call the object. Not that I need much excuse, but I have to be pedantic here just to (hopefully) make it plainer why the expected isn't happening.

If you do a HTTP GET against a URL that's mapped to the FacesServlet, the FacesServlet will dissect the URL and locate the resource necessary to render a JSF view using that resource as a view template. At that point, only set/get methods are being called, basically.

So to execute business logic, you'd have to attach it to one of 3 places:

1. The constructor. Often that doesn't work, since the bean isn't fully initialized yet

2. One of the "set" methods. This can be hazardous, since a given property may be set multiple times on a single request. Also, you can't really depend on all the pre-requisite "set" methods having been called first.

3. A PostConstruct method. In JSF2, you can annotate a method to be invoked after the constructor has been executed and all the "set" methods have been called. However, this is only invoked once, so it works best if the bean is in Request scope.

For what you want, all of these are fairly awkward. A cleaner solution would be to get some outside help:

The ocpsoft PrettyFaces component allows you to easily kit out a JSF webapp to accept "user friendly" URLs such as what you're submitting. It will parse the URL, call setters to inject the parameters into the backing bean, then invoke a method that you specify to process the request. The great thing about it is that not only is it pretty easy to install and configure, but it doesn't require any specialized login in your application code. It's all done POJO-style.

On the other hand, if you're seriously into AJAX, you might want to forgo the manual coding and enlist an extension tagset such as RichFaces. RichFaces makes it easy for JavaScript on JSF pages to invoke JSF functionalities, and can even help with partial page updating. Better yet, in many cases, RichFaces generates all the logic itself, so you can simply declare page elements and it will whip up the required (browser-tuned!) JavaScript code behind the scenes. A lot less work.

Incidentally, I think RichFaces uses jQuery internally to get a lot of its "oomf". You can also combine PrettyFaces and RichFaces. They play well together, especially now that Red Hat/JBoss has the author of PrettyFaces on the payroll.


Customer surveys are for companies who didn't pay proper attention to begin with.
Melinda Savoy
Ranch Hand

Joined: Jun 21, 2005
Posts: 386

After getting some more help from another forum I found out that I had the wrong methods setup in my bean and that I have to get the parameter from the following class in JSF:

FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap().get("parameterName");

I'm moving forward again and appreciate the help from both this forum and the HTML/Javascript forum.

Hopefully I can get this done to get my page completed.

Thanks again.
 
Don't get me started about those stupid light bulbs.
 
subject: Please help - More specific question on JSF + jQuery
 
Similar Threads
Handling Exception/Runtime Exception in JSF
Question on jQuery ajax functionality & troubleshooting called bean in JSF page
Questions (1) accessing managed bean directly and (2) using jQuery ajax functionality in my JSF page
trying to populate hidden field on my JSF form using jQuery
using h:message for non error