This week's giveaway is in the Spring forum.
We're giving away four copies of Learn Spring Security (video course) and have Eugen Paraschiv on-line!
See this thread for details.
Win a copy of Learn Spring Security (video course) this week in the Spring forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Please help - More specific question on JSF + jQuery

 
Melinda Savoy
Ranch Hand
Posts: 387
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 17989
47
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Melinda Savoy
Ranch Hand
Posts: 387
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic