1

I am doing a POST-request using jQuery which seems to succeed. But how can I work with that on server side and modify the response?
Do I need another servlet because the Faces Servlet is just not designed to deal with this?

$.ajax({type:'POST', data:{"status":status}, success: function(response) {
    alert("Qapla'");
}});

It is used for the following process:

  1. user inputs address and hits commandButton which invokes JS
  2. JS retrieves geodata using google maps and sends it to server (which I am considering to use the above code for)
  3. the servers responds sending some close places from database
  4. JS retrieves exact distances using google maps again and sends them to server
  5. server redirects client to next page with results

There is one case where a failing validation for the used inputText might be needed: At point 2 the server rates the geodata as not valid.

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
Lester
  • 1,830
  • 1
  • 27
  • 44

1 Answers1

4

If sending the ajax POST by usual JSF means (UICommand component, jsf.ajax.request(), etc, in flavor of <h:commandButton>, <p:remoteCommand>, <o:commandScript>, etc) is really not an option for some reason left unspecified in your question, then you'd indeed better create a separate servlet or even JAX-RS or JAX-WS webservice listening on those requests and returning e.g. XML, JSON, etc. JSF is a HTML form based MVC framework not a web service framework.

You only need to take into account that you deal properly with JSF view state when you manipulate the HTML representation of JSF components afterwards. E.g. when you use custom JS/ajax to enable a disabled HTML button as generated by <h:commandButton> without involving/notifying JSF, then it won't appear as enabled in JSF component state and its action would never be invoked.

See also:

Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • Thank you for your answer. I suppose using `jsf.ajax.request()` doesn't make much sense, but maybe I am wrong. I added some info on the "what for" to the question and would be glad to hear how you consider this. – Lester Jul 16 '13 at 19:11
  • This will bite you hard in long term if you attempt to handle it in "plain" HTML/JS/jQuery while not really understanding the point of JSF. You'd need to fiddle around with populating hidden inputs and using `jsf.ajax.request()` (or `` as you're apparently using PrimeFaces). It'd be easier to use a ready-to-use JSF component for this. Given that you're using PrimeFaces, or at least famliar with it (your question history is evidence), then just grab its `` component. Here's its showcase: http://www.primefaces.org/showcase/ui/gmapHome.jsf – BalusC Jul 16 '13 at 19:14
  • Unfortunately Primesfaces' `` does not allow you to do geocoding or calculate distances for which I use the services of the Google Maps Javascript API. My current solution DOES involve a lot of fiddling with hidden inputs, `` etc. That is why I want to refactor it now. Can you give me some advice on what I have to learn to BE ABLE to do this right? – Lester Jul 16 '13 at 19:46
  • During an approach using a Servlet I realised how much harder this is to integrate with JSF. And as I just realised the whole power of `` (which in contrary to `jsf.ajax.request()` allows you to add callback parameters to the ajax response), I did a much more effective rewrite using this. My new solution works without hidden inputs and uses all the benefits of JSF (with Primefaces). – Lester Jul 27 '13 at 12:51
  • 1
    You're welcome. Note that `jsf.ajax.request()` definitely also supports passing request params from client to server (note: those are not to be called "callback params"). The OmniFaces `` is built around it. See also http://showcase.omnifaces.org/components/commandScript and its source code https://code.google.com/p/omnifaces/source/browse/src/org/omnifaces/component/script/CommandScript.java As to callback params (sending JS variables from server to client), use `RequestContext#addCallbackParam()` in PrimeFaces or `Ajax#data()` in OmniFaces. – BalusC Jul 27 '13 at 12:54