jquery请求后台struts2传递jon

14 篇文章 0 订阅
9 篇文章 0 订阅

    前台jquery请求
    var beanForm = $("form").serialize(); //序列化前台form表单的元素(注意name和javaBean对象字段名字保持一致)
    $.dialog.confirm('你确定要保存所填的数据吗?', function(){
     $.ajax({
         type: "Post", 
         contentType: "application/x-www-form-urlencoded; charset=UTF-8",
         url: "notification/saveNotification.action",
         data: {notificationBean:beanForm},
         dataType: 'json',
         success: function (data) {
          if(data.bool == 'success'){
           $.dialog.success('保存成功');
          }else{
           $.dialog.error('保存失败');
          }
         },
         error : function(){
          $.dialog.error('服务器忙,保存失败');
         }
     });
    })


    //后台action 方法

    public String saveNotification() throws Exception{
 JSONObject jo = null;
 jo = string2Json(notificationBean);
 bean = (TNotification)JSONObject.toBean(jo, TNotification.class);
 if(notfService.save(bean)){
  bool = "success";
 }else{
  bool = "false";
 }
 return "check";
 
}

 

 //将表单提交数据转换成json格式
 public JSONObject string2Json(String stringForForm) throws Exception {
  String form2Json[] = stringForForm.split("&");
  String jsonForStringTemp = "";
  for (int i = 0; i < form2Json.length; i++) {
   int index = form2Json[i].indexOf("=");
   jsonForStringTemp += "\""
     + form2Json[i].substring(0, index)
     + "\""
     + ":"
     + "\""
     + URLDecoder.decode(form2Json[i].substring(index + 1,
       form2Json[i].length()), "UTF-8") + "\"" + ",";
  }
  String jsonForString = "{" + jsonForStringTemp.substring(0,jsonForStringTemp.length()-1) + "}";
  JSONObject jo = JSONObject.fromObject(jsonForString);
  return jo;
 }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值