将数据从一个servlet的JavaScript code在Ajax应用程序? [英] passing data from a servlet to javascript code in an Ajax application?

查看:124
本文介绍了将数据从一个servlet的JavaScript code在Ajax应用程序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个简单的JSP / Servlet的应用程序,我想AJAX功能添加到这个程序。我使用JQuery,但没关系我使用的JavaScript框架。这是我的code:

I have a simple jsp/servlet application and I want to add AJAX feature to this app. I use JQuery , but it doesn't matter what javascript framework I use. This is my code:

<script type="text/javascript">

        function callbackFunction(data){
            $('#content').html(data);
        }
        $('document').ready(function(){

            $('#x').click(function() {
              $.post('/ajax_2/servlet',callbackFunction)

            });
        });
    </script>
    <body>
        <a href="#" id="x">Increase it</a>
        <div id="content"></div>

    </body>
</html>

Servlet的

Servlet

    HttpSession session = request.getSession();
    Integer myInteger = (Integer)session.getAttribute("myInteger");
    if(myInteger == null)
        myInteger = new Integer(0);
    else
        myInteger = new Integer(myInteger+1);
    session.setAttribute("myInteger", myInteger);
    response.getWriter().println(myInteger);

的问题:

我使用的out.print 可以从一个servlet传输数据的JavaScript code(阿贾克斯code),但如果我有一个复杂的结构,如矢量对象或这样的事情,什么是数据传输的最佳方式是什么?怎么样一个 XML 文件, JSON ?是否有任何的特殊的jsp / servlet的库从一个servlet的数据传输到Ajax应用程序?我怎样才能解析这些数据在 callbackFunction参数

I use out.print to transfer data from a servlet to javascript code (ajax code) , but If I have a complex structure such as a Vector of Objects or something like this , what is the best way to transfer the data? what about an XML file , JSON ? Is there any special jsp/servlets library to transfer data from a servlet to ajax application ? How can I parse this data in the callbackFunction ?

推荐答案

最好的办法是使用的 JSON 。有几种Java库,它可以转换fullworthy Java对象为JSON字符串,反之亦然。而且JSON可以在JavaScript访问在一个完全自然的方式,无需转换/来回按摩以另一种格式的数据。

The best way is using JSON. There are several Java libraries which can convert fullworthy Java objects to a JSON string and vice versa. Further JSON can be accessed in Javascript in a fully natural way without converting/massaging the data forth and back in another format.

对于服务器端而言,我强烈建议挑谷歌GSON 作为JSON序列。 GSON是preferred的选择,因为它支持在code单行转换复杂的JavaBeans和数组,集合和他们的地图,JSON,反之亦然无痛苦。它甚至还支持泛型。基本上所有你需要做的是以下内容:

As to the server side part, I strongly recommend to pick Google Gson as JSON serializer. Gson is the preferred choice since it supports converting complex Javabeans and arrays, collections and maps of them to JSON and vice versa without pains in a single line of code. It even supports generics. Basically all you need to do is the following:

String json = new Gson().toJson(object);

检查用户指南详细了解GSON的权力

Check the user guide to learn more about the powers of Gson.

所有的一切,在服务器端以下就足够了:

All with all, the following in the server side is sufficient:

public static void writeJson(HttpServletResponse response, Object object) throws IOException {
    response.setContentType("application/json");
    response.setCharacterEncoding("UTF-8");
    response.getWriter().write(new Gson().toJson(object));
}

这篇关于将数据从一个servlet的JavaScript code在Ajax应用程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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