加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

Ajax同Struts进行JSON格式的数据交换

发布时间:2020-12-15 20:59:36 所属栏目:百科 来源:网络整理
导读:Ajax向服务器提交数据 可以采用$.ajax,$.get或者$.post,这里采用$.post为例子 $.post("jsontest.action",{v1:"12345",v2:"12345"},function(result){ $("p#1").html(result.user.name); $("p#2").html(result.user.age); },"json"); Struts获取Ajax提交的数
  • Ajax向服务器提交数据

可以采用$.ajax,$.get或者$.post,这里采用$.post为例子

$.post("jsontest.action",{v1:"12345",v2:"12345"},function(result){
$("p#1").html(result.user.name);
$("p#2").html(result.user.age);
},"json");

  • Struts获取Ajax提交的数据并给出响应

// 获取request对象

HttpServletRequest req = ServletActionContext.getRequest();

// 获取数据

String v1 = req.getParameter("v1");

String v2 = req.getParameter("v2");

上面这部分可以省去,因为,struts会自动设置属性值。

// 获取response对象

HttpServletResponse response = ServletActionContext.getResponse();
PrintWriter out = response.getWriter();

// 响应

User user = new User();
user.setAge(15);
user.setName("l11eo");

JSONObject obj = new JSONObject();
obj.accumulate("user",user);
out.print(obj.toString());
out.flush();
out.close();


returnNONE;

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读