博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
struts2 jquery ajax 实现异步页面刷新
阅读量:4340 次
发布时间:2019-06-07

本文共 2441 字,大约阅读时间需要 8 分钟。

1.处理ajax的异步请求

 

public class JsonAction extends ActionSupport implements ServletRequestAware{    private static final long serialVersionUID = 1L;        private HttpServletRequest request;    private String result; //用于返回json 的数据结果,必须有getter and setter方法    public void setServletRequest(HttpServletRequest arg0) {        this.request = arg0;    }    public String getResult() {        return result;    }    public void setResult(String result) {        this.result = result;    }        /**     * 处理ajax请求     * @return success     */    public String excuteAjax(){        try {            //获取数据,单个获取数据            String word= request.getParameter("word");            int wordcount= Integer.parseInt(request.getParameter("wordcount"));             //将数据存储在map里,再转换成json类型数据,也可以自己手动构造json类型数据            Map
map = new HashMap
(); map.put("word", word); map.put("wordcount",wordcount); JSONObject json = JSONObject.fromObject(map);//将map对象转换成json类型数据 result = json.toString();//给result赋值,传递给页面                 //手动构造json数据类型        result = {\'word\':\'"+word+"\'};{\'wordcount\':\'"+wordcount+"\'}"       //查询结果集list数据 RowSet rs = twImpl.queryAllBook(dbId); List
> list = new ArrayList
>(); while(rs != null && rs.next()) { Map
map=new HashMap
(); map.put("word", rs.getString("word")); map.put("wordcount", rs.getInt("wordcount")); list.add(map); } JSONArray jsarry = new JSONArray(); jsarry=JSONArray.fromObject(list); result=jsarry.toString(); } catch (Exception e) { e.printStackTrace(); } return "success"; } }

2.struts.xml的配置文件

result

 3.ajax.js文件

$(function(){$.ajax({    cache:false,    async:false,    type:"post",    dataType:"json",     url:'JsonAction_excuteAjax.action?timestamp='+new Date().getTime().toString(),	 data:{		word:word,		word:wordcount //此处不能加‘,’号		  },	 error:function(){		alert("error occured!!!");	 },         success:function(data){		var test=eval("("+data+")");                //非循环的结果获得		var word= eval("("+data+")").word;		var wrodcount= eval("("+data+")").wordcount;                //循环结果集		$.each(test,function(i)		{			desc += '
  • '+test[i].word+'('+test[i].wordcount+'%)
  • '; }); } });});

     

    转载于:https://www.cnblogs.com/cyanqx/p/3477273.html

    你可能感兴趣的文章
    Hive实现oracle的Minus函数
    查看>>
    秒杀多线程第四篇 一个经典的多线程同步问题
    查看>>
    RocketMQ配置
    查看>>
    蚂蚁金服井贤栋:用技术联手金融机构,形成服务小微的生态合力
    查看>>
    端口号大全
    查看>>
    机器学习基石笔记2——在何时可以使用机器学习(2)
    查看>>
    POJ 3740 Easy Finding (DLX模板)
    查看>>
    MySQL 处理重复数据
    查看>>
    关于typedef的用法总结(转)
    查看>>
    【strtok()】——分割字符串
    查看>>
    Linux下安装rabbitmq
    查看>>
    曹德旺
    查看>>
    【转】判断点在多边形内(matlab)
    查看>>
    java基础之集合:List Set Map的概述以及使用场景
    查看>>
    Python 线程 进程 协程
    查看>>
    iOS语言中的KVO机制
    查看>>
    excel第一次打开报错 向程序发送命令时出错 多种解决办法含终极解决方法
    查看>>
    响应式web设计之CSS3 Media Queries
    查看>>
    实验三
    查看>>
    机器码和字节码
    查看>>