用JAVA编写Ajax代码

发布于 2024-08-10 03:40:45 字数 130 浏览 16 评论 0原文

我想用java编写ajax代码,即我想使用ajax的功能而不使用ajax。我正在寻找一些可以做到这一点的JAVA API。

就像我们通过JAVA程序发布网页数据一样,我想通过JAVA程序执行ajax操作。

请建议。

I want to write ajax code in java i.e. i want use the functionality of ajax without using ajax. I'm looking for some API of JAVA which can do so.

Like we post data of a web page through JAVA program similarly I want to perform operation of ajax through JAVA program.

Please suggest.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(9

吻风 2024-08-17 03:40:45

Google Web Toolkit 是用于编写 AJAX 应用程序的纯 Java 框架。

Google Web Toolkit is Java-only framework for writing AJAX aplications.

你是暖光i 2024-08-17 03:40:45

Echo 是 gwt 的替代品

Echo is an alternative to gwt

尛丟丟 2024-08-17 03:40:45

您可以使用 jQuery 来实现此目的。在 jQuery 中,你有一个很棒的 form 插件,它可以不显眼地将现有表单更改为 ajaxform。

HTML(在 JSP 中):

<form id="myform" action="myservlet" method="post"> 
    <input type="text" name="foo"> 
    <input type="submit"> 
</form>
<div id="message">${message}</div>

JS((在)直接在 JSP 中):

$('#myform').ajaxForm({
    success: function(message) { $('#message').text(message); }
});

Java((在)直接在 myservlet 后面的 Servlet 的 doPost() 方法中):

String foo = request.getParameter("foo");
String message = "You entered 'bar': " + ("bar".equals(foo) ? "yes" : "no");

if ("XMLHttpRequest".equals(request.getHeader("x-requested-with"))) {
    // Ajax request.
    response.getWriter().write(message);
} else {
    // Normal request.
    request.setAttribute("message", message);
    request.getRequestDispatcher("page.jsp").forward(request, response);
}

如果您想要更进一步,您可以在 Servlet 中使用 Gson 来转换完整的 Java对象到 Javascript 对象表示法 (JSON)。这样您就可以在 Javascript 中以类似 javabean 的方式访问数据。

You can use jQuery for this. In jQuery you have the great form plugin which unobtrusively changes an existing form into an ajaxform.

HTML (in JSP):

<form id="myform" action="myservlet" method="post"> 
    <input type="text" name="foo"> 
    <input type="submit"> 
</form>
<div id="message">${message}</div>

JS ((in)directly in JSP):

$('#myform').ajaxForm({
    success: function(message) { $('#message').text(message); }
});

Java ((in)directly in doPost() method of the Servlet behind myservlet):

String foo = request.getParameter("foo");
String message = "You entered 'bar': " + ("bar".equals(foo) ? "yes" : "no");

if ("XMLHttpRequest".equals(request.getHeader("x-requested-with"))) {
    // Ajax request.
    response.getWriter().write(message);
} else {
    // Normal request.
    request.setAttribute("message", message);
    request.getRequestDispatcher("page.jsp").forward(request, response);
}

If you want to get some steps further, you can use Gson in Servlet to convert complete Java objects to Javascript object notation (JSON). This way you can access the data the javabean-like way in Javascript.

愛放△進行李 2024-08-17 03:40:45

如果您的应用程序在浏览器上运行并且是 Web 应用程序,则可以使用 GWT。如果您的应用程序是核心java应用程序..您可以简单地创建一个 HttpURLConnection 并使用它。

If your application is running on browser and its a web application, you can go with GWT. If your application is a core java application.. you can simply create a HttpURLConnection and use it.

忱杏 2024-08-17 03:40:45

JavaServer Faces (JSF) 2.0 应该能够在幕后使用 AJAX 对页面进行部分更新。在我看来,这似乎就是你所需要的。

我认为现在运行支持 JSF 2.0 的服务器的最简单方法是使用最新版本的 Glassfish。

JavaServer Faces (JSF) 2.0 should be able to do partial updates of a page in place, using AJAX under the covers. It sounds to me as if that is what you need.

I think the easiest way to get a JSF 2.0 capable server running right now, is using the latest version of Glassfish.

陈年往事 2024-08-17 03:40:45

有很多基于 Java 的 AJAX 框架。

甚至更多 JAVA AJAX 框架

There are plenty of Java based AJAX frameworks out there.

  • Apache Struts [complex AJAX-tags (by integrating DOJO-toolkit)]

  • Direct Web Remoting is a framework for calling Java methods directly from Javascript code.

  • Guise™ Framework - elegant server-side component architecture that doesn't require developers to write HTML or JavaScript

  • GWT - Java software development framework that makes writing AJAX applications

  • JAXCENT - Just-Java Framework and API for AJAX

  • IT Mill - Ajax framework for developing web applications for with Java language at server-side

and even more JAVA AJAX Frameworks

抱猫软卧 2024-08-17 03:40:45

我建议您查看 DWR - Easy Ajax for Java

DWR 是一个 Java 库,它支持
服务器上的 Java 和服务器上的 JavaScript
浏览器进行交互和调用
其他尽可能简单。

DWR 是用于 Java 的 Easy Ajax

它使您能够以最小的努力对您的 Web 应用程序进行 ajax 化。
它不是一个全新的网络框架;它只专注于 ajaxification,允许您保留和使用现有的 Web 框架。

如果您想使用像 JSF 这样的“较重”的 Web 框架,可以使用支持 ajax 的 JSF 框架,例如 IceFacesRichFaces 提供开箱即用的 ajax。

I suggest you take a lok at DWR - Easy Ajax for Java:

DWR is a Java library that enables
Java on the server and JavaScript in a
browser to interact and call each
other as simply as possible.

DWR is Easy Ajax for Java

It enables you to ajax-ify your web application with minimal effort.
It is not a complete new web-framework; it focuses solely on ajaxification, allowing you to keep and use your existing web framework.

If you want to use a "heavier" web framework like JSF, there exist ajax-ready JSF frameworks like IceFaces and RichFaces with provide ajax out-of-the-box.

英雄似剑 2024-08-17 03:40:45

您可以将表单提交给servlet,当servlet响应时,您可以使用jquery或原型javascript框架通过Ajax调用函数从服务器获取数据。
我尝试了一下,运行很顺利!

祝你好运!

you can submit form to servlet, when servlet response, you using jquery or prototype javascript framwork to get data from server by using Ajax call function.
I tried it and it run smoothly!

Good luck!

夜访吸血鬼 2024-08-17 03:40:45

快速回答是 GWT。然而,你为什么要这样做呢? JavaScript 与 Java 类似。如果您了解 Java,那么您可以轻松编写 JavaScript 代码。使用 JavaScript 的优点是你会更加灵活,不会被单一工具所束缚。使用 GWT 生成 JavaScript (AJAX) 代码并不自然。

The quick answer is GWT. However, why would you want to do this? JavaScript is similar to Java. If you know Java, then you can easily write JavaScript code. The advantage of using JavaScript would be that you would be more versatile and would not be locked into a single tool. Using GWT to generate JavaScript (AJAX) code is not natural.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文