如何将手动jQuery Ajax请求发送到JSF应用程序? [英] How to send manual jQuery Ajax request to JSF application?

查看:195
本文介绍了如何将手动jQuery Ajax请求发送到JSF应用程序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用jQuery成功执行POST-请求.但是如何在服务器端使用它并修改响应?
我是否需要另一个Servlet,因为Faces Servlet并非旨在解决这个问题?

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'");
}});

它用于以下过程:

  1. 用户输入地址并点击commandButton从而调用JS
  2. JS使用Google地图检索地理数据并将其发送到服务器(我正在考虑将其用于上述代码)
  3. 服务器响应发送一些靠近数据库的位置
  4. JS再次使用Google地图检索精确距离并将其发送到服务器
  5. 服务器将客户端重定向到包含结果的下一页
  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

在某些情况下,可能需要对使用的inputText进行失败的验证:在第2点,服务器将地理数据评为无效.

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.

推荐答案

如果通过通常的JSF方式(UICommand组件,jsf.ajax.request()等,以<h:commandButton><p:remoteCommand><o:commandScript>等)由于某些原因在您的问题中未指定,因此实际上不是一个选项,那么您确实最好创建一个单独的servlet甚至JAX-RS或JAX-WS Web服务来侦听那些请求并返回例如XML,JSON等.JSF是基于HTML表单的MVC框架,而不是Web服务框架.

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.

您随后只需要考虑在处理JSF组件的HTML表示时正确处理了JSF视图状态.例如.当您使用自定义JS/ajax启用由<h:commandButton>生成的禁用HTML按钮而不涉及/通知JSF时,它在JSF组件状态下将不会显示为已启用,并且其操作将永远不会被调用.

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.

  • 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?

这篇关于如何将手动jQuery Ajax请求发送到JSF应用程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆