struts2学习笔记--使用struts2插件实现ajax处理(返回json数据)

简介:   贴一个简单的例子,通过jquery的post调用action,定义一个对象User,有name和age属性,实例化几个对象,以json的格式返回到jsp,在前台页面显示出来,模拟用户列表.   导入相关jar包: ezmorph-1.

  贴一个简单的例子,通过jquery的post调用action,定义一个对象User,有name和age属性,实例化几个对象,以json的格式返回到jsp,在前台页面显示出来,模拟用户列表.

  •   导入相关jar包:
  1. ezmorph-1.0.6.jar
  2. json-lib-2.3-jdk15.jar
  3. struts2-json-plugin-2.3.16.1.jar
  • Action代码:
package com.wang.action;

import java.util.ArrayList;
import java.util.List;

import com.opensymphony.xwork2.Action;
import com.wang.entity.User;

import net.sf.json.JSONArray;

public class JsonAction {

    private JSONArray root;

    public String execute(){
        List<User> list=new ArrayList<User>();
        list.add(new User("wang",20));
        list.add(new User("yong",22));
        list.add(new User("guo",23));
        root=JSONArray.fromObject(list);
        System.out.println("json="+root.toString());
        return Action.SUCCESS;
    }
    public JSONArray getRoot() {
        return root;
    }

    public void setRoot(JSONArray root) {
        this.root = root;
    }
    
    
}
  •  jsp页面:
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>My JSP 'ajax.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="jquery/jquery.min.js"></script>
    <script type="text/javascript">
        $(function(){
            $('#btn1').click(function(){
                $.post("json.action",function(data){
                    var html="";
                    for(var i=0;i<data.length;i++){
                        html+="<tr><td>"+data[i].name+"</td><td>"+data[i].age+"</td></tr>";
                    }
                    $('#content').html(html);
                });
            });
            $('#btn2').click(function(){
            //    alert("a");
                $('#content').css("display","none");
            });
            
        });
    </script>
  </head>
  
  <body>
    <input type="button" name="btn" id="btn1" value="获取json"/><br>
    <table width="80%" align="center">
        <tr>
            <td>姓名</td>
            <td>年龄</td>
        </tr>
        <tbody id="content">
        
        </tbody>
    </table>
     <input type="button" name="btn" id="btn2" value="隐藏json信息"/><br>
  </body>
</html>
  • struts.xml:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd">
<struts>
<!--注意这里继承的json-default,间接地也继承struts-default-->    
    <package name="default" extends="json-default" namespace="/">
        <action name="json" class="com.wang.action.JsonAction">
            <result type="json">
                <param name="root">root</param>
            </result>
        </action>
    </package>
</struts>    

 

相关文章
|
2天前
|
存储 JSON 数据处理
从JSON数据到Pandas DataFrame:如何解析出所需字段
从JSON数据到Pandas DataFrame:如何解析出所需字段
14 1
|
2天前
|
XML JSON API
转Android上基于JSON的数据交互应用
转Android上基于JSON的数据交互应用
10 1
|
2天前
|
JSON JavaScript Java
从前端Vue到后端Spring Boot:接收JSON数据的正确姿势
从前端Vue到后端Spring Boot:接收JSON数据的正确姿势
25 0
|
2天前
|
JSON 数据格式 Python
Python标准库中包含了json模块,可以帮助你轻松处理JSON数据
【4月更文挑战第30天】Python的json模块简化了JSON数据与Python对象之间的转换。使用`json.dumps()`可将字典转为JSON字符串,如`{&quot;name&quot;: &quot;John&quot;, &quot;age&quot;: 30, &quot;city&quot;: &quot;New York&quot;}`,而`json.loads()`则能将JSON字符串转回字典。通过`json.load()`从文件读取JSON数据,`json.dump()`则用于将数据写入文件。
18 1
|
2天前
|
JSON 数据格式 Python
Python处理JSON数据
【4月更文挑战第30天】该内容介绍了Python处理JSON数据的三个方法:1)使用`json.loads()`尝试解析字符串以验证其是否为有效JSON,通过捕获`JSONDecodeError`异常判断有效性;2)通过`json.dumps()`的`indent`参数格式化输出JSON数据,使其更易读;3)处理JSON中的日期,利用`dateutil`库将日期转换为字符串进行序列化和反序列化。
23 4
|
2天前
|
XML 前端开发 JavaScript
jQuery中ajax如何使用
jQuery中ajax如何使用
23 0
N..
|
2天前
|
XML JSON 前端开发
jQuery实现Ajax
jQuery实现Ajax
N..
23 1
|
6月前
|
XML 前端开发 JavaScript
什么是Ajax和jquery
什么是Ajax和jquery
48 0
|
2天前
|
JSON 前端开发 Java
利用Spring Boot处理JSON数据实战(包括jQuery,html,ajax)附源码 超详细
利用Spring Boot处理JSON数据实战(包括jQuery,html,ajax)附源码 超详细
63 0
|
2天前
|
敏捷开发 JavaScript 前端开发
❤❤❤【Vue.js最新版】sd.js基于jQuery Ajax最新原生完整版for凯哥API版本❤❤❤
❤❤❤【Vue.js最新版】sd.js基于jQuery Ajax最新原生完整版for凯哥API版本❤❤❤