How to send manual jQuery Ajax request to JSF application?

这一生的挚爱 提交于 2019-12-20 04:39:09

问题


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.


回答1:


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:

  • How to use Servlets and Ajax?
  • How to generate JSON response from JSF?
  • How to invoke a JSF managed bean on a HTML DOM event using native JavaScript?
  • What is the need of JSF, when UI can be achieved from CSS, HTML, JavaScript, jQuery?


来源:https://stackoverflow.com/questions/17681055/how-to-send-manual-jquery-ajax-request-to-jsf-application

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!