struts2之ajax(json)或者struts2之http(json)记录

struts2+ajax+json或者struts2+http+json


1 首先:struts的配置文件:
<package name="myhttp" namespace="/" extends="struts-default">
  <action name="crm_cust" class="crmCustomerAction">
  </action>
</package>


2 然后后台代码:
public String findOneByHttp(){




。。。。
Map<String, Object> jsonMap=new HashMap<String,Object>();
jsonMap.put("customer", customer_);//需要传递的参数数据
com.crm.tools.ToAjaxJsonString.ajaxJson(jsonMap);//将数据转化为json格式的数据
return null;
}


3 赋值和copy类:ToAjaxJsonString
/**
* @Title: ToAjaxJsonString.java.
* @Package com.crm.tools
* Copyright: Copyright (c) 2016-9-13
* Company:湖南创发科技有限责任公司
* @author Comsys-xcl
* @date 2016-9-13 下午01:41:05
* @version V1.0
*/
package com.crm.tools;


import java.io.IOException;
import java.util.Map;


import javax.servlet.http.HttpServletResponse;


import net.sf.json.JSONObject;


import org.apache.struts2.ServletActionContext;


/**
* <p>Class Name: ToAjaxJsonString.</p>
* <p>Description: 类功能说明</p>
* <p>Sample: 该类的典型使用方法和用例</p>
* <p>Author: xcl</p>
* <p>Date: 2016-9-13</p>
* <p>Modified History: 修改记录,格式(Name)  (Version)  (Date) (Reason & Contents)</p>
*/
public class ToAjaxJsonString {


   public static String ajaxJson(Map<String, Object> jsonMap) {  
JSONObject jsonObject = JSONObject.fromObject(jsonMap);//将Java对象(如:Map数据,ArrayList等)转换为JSONObject类型  
//首先jsonObject.toString()将JSONObject类型数据转换为json字符串!  
return ajax(jsonObject.toString(), "application/json");//然后调用ajax()方法将json格式字符串以Ajax形式返回前端  
   }
   
 //将json格式字符串以AJAX形式返回,否则返回null  
   public static String ajax(String content, String type) {  
try {  
   HttpServletResponse response = ServletActionContext.getResponse();  
   response.setContentType(type + ";charset=UTF-8");  
   response.setHeader("Pragma", "No-cache");  
   response.setHeader("Cache-Control", "no-cache");  
   response.setDateHeader("Expires", 0);  
   response.getWriter().write(content);  
   response.getWriter().flush();  
} catch (IOException e) {  
   e.printStackTrace();  
}
return null;  
   }
}


4 通过前台ajax调用或者浏览器地址调用:
例如:通过浏览器调用:
http://localhost:8080/MyCrm/crm_cust!findOneByHttp?id=6


得到json的结果集。
{"customer":
{
"paramSysparamByCustomerindustryId":null,
"hrEmployeeByCreateId":null,
"lastfollow":null,
"tel":"029-89425112",
"cityName":"西安",
"remarks":"带来可观的发那个肯定是女生看见内需政策",
"provinceName":"陕西",
"serialnumber":"222222222222231",
"customer":"新东方",
"employeeName":"贺轩",
"hrDepartment":null,
"id":6,
"sourceName":"客户上门",
"crmOrders":[],
"descripe":"",
"paramCityByCityId":null,
"levelName":"B",
"paramCityByProvincesId":null,
"createDate":null,
"paramSysparamByCustomerlevelId":null,
"createDateStr":"2015-1-9 0:00:00",
"site":"www.xindf.com",
"crmInvoices":[],
"paramSysparamByCustomersourceId":null,
"crmContracts":[],
"hrEmployeeByEmployeeId":null,
"isdelete":0,
"privatecustomer":"公客",
"departmentName":"业务部",
"typeName":"有意向",
"paramSysparamByCustomertypeId":null,
"crmFollows":[],
"crmReceives":[],
"address":"西安西郊",
"deleteTime":null,
"lastfollowStr":"",
"crmContacts":[]
}}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值