一、使用XML传递

1、页面展示getXML.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
 
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
 
    <title>My JSP 'getXML.jsp' starting page</title>
 
    <script type="text/javascript" src="scripts/jquery-1.4.4.js"></script>
 
    <meta http-equiv="pragma" content="no-cache">
    <meta http-equiv="cache-control" content="no-cache">
    <meta http-equiv="expires" content="0">   
    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    <meta http-equiv="description" content="This is my page">
    <!--
    <link rel="stylesheet" type="text/css" href="styles.css">
    -->
 
    <script type="text/javascript">
 
    function getInfo()
    {
        $.post("getXMLAction.action",
        {
            name: $("#name").val()//像后台getXMLAction.action中传递name值
        },function(returnedData, status)
        {
            var id = $(returnedData).find("id").text();//使用find函数取得后台传递的XML信息
            var name = $(returnedData).find("name").text();
            var age = $(returnedData).find("age").text();
            var address = $(returnedData).find("address").text();
 
            var html = "<table width='60%' border='1' align='center'><tr><th>id</th><th>name</th><th>age</th><th>address</th><tr align='center'><td>" + id + "</td><td>" + name + "</td><td>" + age + "</td><td>" + address + "</td></tr></table>";
 
            $("#theBody table:eq(0)").remove();//表示每次将table表格中的第一行清除掉,进行添加,显示为每次显示一条结果
 
            $("#theBody").append(html);
        });       
    }
    </script>
 
  </head>
 
    <body id="theBody">
 
   <select id="name">
 
    <option value="zhangsan">zhangsan</option>
    <option value="lisi">lisi</option>
 
   </select>  
 
    <input type="button" value="get information" onclick="getInfo();">
  </body>
</html>
2、配置struts
<action name="getXMLAction" class="com.shengsiyuan.action.xml.GetXMLAction">
</action>
3、bean对象Person.java
package com.shengsiyuan.action.xml;
 
public class People
{
    private int id;
 
    private String name;
 
    private int age;
 
    private String address;
 
    public int getId()
    {
        return id;
    }
 
    public void setId(int id)
    {
        this.id = id;
    }
 
    public String getName()
    {
        return name;
    }
 
    public void setName(String name)
    {
        this.name = name;
    }
 
    public int getAge()
    {
        return age;
    }
 
    public void setAge(int age)
    {
        this.age = age;
    }
 
    public String getAddress()
    {
        return address;
    }
 
    public void setAddress(String address)
    {
        this.address = address;
    }
}
4、GetXMLAction.java用于处理请求和返回请求
);
        people1.setName();
        people1.setAddress();
        people2.setName();
        people2.setAddress("tianjin");
 
        Document document = DocumentHelper.createDocument();//dom4j中的document对象
 
        Element rootElement = document.addElement("persons");//使用element来定义XML
 
        rootElement.addComment("This is comment!!");
 
        Element e = rootElement.addElement("person");//根元素为person
 
        Element idElement = e.addElement("id");//子元素对应关系
        Element nameElement = e.addElement("name");
        Element ageElement = e.addElement("age");
        Element addressElement = e.addElement("address");
 
        if("zhangsan".equals(name))
        {
            idElement.setText(people1.getId() + "");
            nameElement.setText(people1.getName());
            ageElement.setText(people1.getAge() + "");
            addressElement.setText(people1.getAddress());
        }
        else
        {
            idElement.setText(people2.getId() + "");
            nameElement.setText(people2.getName());
            ageElement.setText(people2.getAge() + "");
            addressElement.setText(people2.getAddress());
        }
 
        HttpServletResponse response = ServletActionContext.getResponse();
 
        response.setContentType("text/xml; charset=utf-8");
        response.setHeader("cache-control", "no-cache");
 
        PrintWriter out = response.getWriter();
 
        OutputFormat format = OutputFormat.createPrettyPrint();
        format.setEncoding("utf-8");
 
        XMLWriter writer = new XMLWriter(out, format);
 
        writer.write(document);
 
        out.flush();
        out.close();
 
        return null;//返回空值,这里对应struts处无result对应的返回值
    }
}
5、对应的XML输出信息
 

二、使用json异步获取信息

1、页面getJson.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
 
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
 
    <title>My JSP 'json.jsp' starting page</title>
 
    <meta http-equiv="pragma" content="no-cache">
    <meta http-equiv="cache-control" content="no-cache">
    <meta http-equiv="expires" content="0">   
    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    <meta http-equiv="description" content="This is my page">
    <!--
    <link rel="stylesheet" type="text/css" href="styles.css">
    -->
    <script type="text/javascript" src="scripts/jquery-1.4.4.js"></script>
 
    <script type="text/javascript">
 
    $(function()
    {
        $("#button1").click(function()
        {
            $.post("getJsonAction2.action",{name: $("#name").val()},
                function(returnedData, status)
            {
                var people = returnedData;
 
                var id = people.id;
                var name = people.name;
                var age = people.age;
                var address = people.address;
 
                var html = "<table width='60%' border='1' align='center'><tr><th>id</th><th>name</th><th>age</th><th>address</th><tr align='center'><td>" + id + "</td><td>" + name + "</td><td>" + age + "</td><td>" + address + "</td></tr></table>";
 
                $("#theBody table:eq(0)").remove();
 
                $("#theBody").append(html);
 
            });           
        });
    });
 
    </script>
 
 
  </head>
 
  <body id="theBody">
 
   <select id="name">
 
    <option value="zhangsan">zhangsan</option>
    <option value="lisi">lisi</option>
 
   </select
 
 
    <input type="button" value="get json content from server" id="button1">
 
  </body>
</html>

2、struts控p制层
        <action name="getJsonAction" class="com.shengsiyuan.action.json.GetJsonAction">
        </action>
3、bean对象Person.java见使用XML获取后台信息
4、GetJsonAction.java处理信息的action
);
        people.setName(name);
        people.setAge();
        people.setAddress("beijing");
 
        Gson gson = new Gson();
 
        String result = gson.toJson(people);
 
        HttpServletResponse response = ServletActionContext.getResponse();
 
        response.setContentType("application/json; charset=utf-8");
        response.setHeader("cache-control", "no-cache");
 
        PrintWriter out = response.getWriter();
 
        out.print(result);
 
        out.flush();
        out.close();
 
        return null;
    }
}
5、以下是Json打印出的json数据

三、使用struts插件获得json数据信息

 
此插件解压缩中对应xml文件信息如下:返回一个json的结果于对应的类进行过处理,而且还定义啦一个json拦截器
 
1、页面中和以上页面相同
2、struts配置
<package name="struts2_ajax" extends="json-default"><!-- 其中更改继承为json-default-->
<action name="getJsonAction2" class="com.shengsiyuan.action.json.GetJsonAction2">
            <result name="success" type="json">
 
                <param name="excludeProperties">address</param><!--这里表示你不想传递到页面的变量-->
 
            </result>
        </action>
</package>
3、GetJsonAction2.java处理​信息的action
;
        ;
        this.address = "beijing";
 
        return SUCCESS;//由于使用了struts插件我们这里返回success
    }
}

Struts2实现异步调用机制详细剖析(XML和JSON)的更多相关文章

  1. 全面剖析XML和JSON

    1.定义介绍 (1).XML定义扩展标记语言 (Extensible Markup Language, XML) ,用于标记电子文件使其具有结构性的标记语言,可以用来标记数据.定义数据类型,是一种允许 ...

  2. Direct3D Draw函数 异步调用原理解析

    概述 在D3D10中,一个基本的渲染流程可分为以下步骤: 清理帧缓存: 执行若干次的绘制: 通过Device API创建所需Buffer: 通过Map/Unmap填充数据到Buffer中: 将Buff ...

  3. Android异步消息处理机制

    安卓子线程无法直接更改UI,所以需要异步消息处理机制来解决 <?xml version="1.0" encoding="utf-8"?><Li ...

  4. silverlight——多次异步调用的顺序执行

    遇到这样一个功能需求,对于后台的同一个服务调用多次,但要求传入的参数能够再一个执行完之后再进行另一个参数的执行. 由于silverlight支持的是异步调用机制,故无法控制服务调用何时返回.那么如果使 ...

  5. 【转】Zookeeper-Watcher机制与异步调用原理

    声明:本文转载自http://shift-alt-ctrl.iteye.com/blog/1847320,转载请务必声明. Watcher机制:目的是为ZK客户端操作提供一种类似于异步获得数据的操作. ...

  6. Zookeeper-Watcher机制与异步调用原理

    转载于:http://shift-alt-ctrl.iteye.com/blog/1847320 Watcher机制:目的是为ZK客户端操作提供一种类似于异步获得数据的操作. 1)在创建Zookeep ...

  7. 并发编程 - 线程 - 1.线程queue/2.线程池进程池/3.异步调用与回调机制

    1.线程queue :会有锁 q=queue.Queue(3) q.get() q.put() 先进先出 队列后进先出 堆栈优先级队列 """先进先出 队列"& ...

  8. Python Django 协程报错,进程池、线程池与异步调用、回调机制

    一.问题描述 在Django视图函数中,导入 gevent 模块 import gevent from gevent import monkey; monkey.patch_all() from ge ...

  9. 13 并发编程-(线程)-异步调用与回调机制&进程池线程池小练习

    #提交任务的两种方式 #1.同步调用:提交完任务后,就在原地等待任务执行完毕,拿到结果,再执行下一行代码,导致程序是串行执行 一.提交任务的两种方式 1.同步调用:提交任务后,就在原地等待任务完毕,拿 ...

随机推荐

  1. http & json

    TCP(传输层协议) (1) 面向连接  (2) 可靠的  (3) 基于字节流的   连接建立阶段: 客户端 ------->SYN                 服务端(服务器被动打开) 客 ...

  2. C语言头文件

    最近在工作当中遇到了一点小问题,关于C语言头文件的应用问题,主要还是关于全局变量的定义和声明问题.学习C语言已经有好几年了,工作使用也近半年了,但是对于这部分的东西的确还没有深入的思考过.概念上还是比 ...

  3. 最小点集覆盖/HDU2119

    题目连接 先试一下题/?/ 最小点集覆盖=最大匹配 /*根据i.j建图,跑一边最大匹配 */ #include<cstdio> #include<cstring> using ...

  4. Java中常见的5种WEB服务器介绍

    这篇文章主要介绍了Java中常见的5种WEB服务器介绍,它们分别是Tomcat.Resin.JBoss.WebSphere.WebLogic,需要的朋友可以参考下 Web服务器是运行及发布Web应用的 ...

  5. Android手机图片适配问题

    需求:今天在做ListView的时候遇到一个问题,就是ListView中加载图片的时候.有些图片的大小比较大,所以会出现图片显示不充分的问题. 首先,再不做任何处理的情况下,大小是这样的.宽度是Wra ...

  6. Java中的值栈

    OGNL表示式使用 和 值栈 OGNL是Object Graphic Navigation Language(对象图导航语言)的缩写,它是一个开源项目. Struts2框架使用OGNL作为默认的表达式 ...

  7. push以及pop,shift,unshift

    压入数组:往数组后面加:push         arr.push()返回值为添加后数组的长度 往数组前面加:unshift     arr.unshift()返回值为添加后数组的长度 拿出数组:拿掉 ...

  8. Linux -- ls只显示目录

    ls没有直接显示目录的选项, 不过根据目录和文件显示的差异,可以搭配grep来实现 方法1: ll | grep "^d" 方法2: ls -F | grep$ "/$& ...

  9. PAT (Advanced Level) 1102. Invert a Binary Tree (25)

    简单题. #include<cstdio> #include<cstring> #include<cmath> #include<vector> #in ...

  10. Android如何使用API

    转自:http://www.cnblogs.com/vanezkw/archive/2012/07/03/2574559.html 本文针对Android开发如何使用API文档进行一些经验分享. 1. ...