jquery请求后台struts2传递jon

    前台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;
 }

你可能感兴趣的:(jquery请求后台struts2传递jon)