java代码用于返回JSON或者XML数据(extJs)
package com.hrm.util;
import java.util.ArrayList;
import java.util.List;
import net.sf.json.JSONObject;
import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.io.xml.DomDriver;
/**
* Title: Ext JS 辅助类
* Description: 该类用于转换java对象为XML文件格式或JSON文件格式
* @author 葛昊
* @time: 2009年2月5日11:10:54
*/
public class ExtHelper {
/**
* 通过List生成XML数据
* @param recordTotal 记录总数,不一定与beanList中的记录数相等
* @param beanList 包含bean对象的集合
* @return 生成的XML数据
*/
@SuppressWarnings("unchecked")
public static String getXmlFromList(long recordTotal , List beanList) {
Total total = new Total();
total.setResults(recordTotal);
List results = new ArrayList();
results.add(total);
results.addAll(beanList);
XStream sm = new XStream(new DomDriver());
for (int i = 0; i < results.size(); i++) {
Class c = results.get(i).getClass();
String b = c.getName();
String[] temp = b.split("\\.");
sm.alias(temp[temp.length - 1], c);
}
String xml = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n" + sm.toXML(results);
return xml;
}
/**
* 通过List生成XML数据
* @param beanList 包含bean对象的集合
* @return 生成的XML数据
*/
@SuppressWarnings("unchecked")
public static String getXmlFromList(List beanList){
return getXmlFromList(beanList.size(),beanList);
}
/**
* 通过List生成JSON数据
* @param recordTotal 记录总数,不一定与beanList中的记录数相等
* @param beanList 包含bean对象的集合
* @return 生成的JSON数据
*/
@SuppressWarnings("unchecked")
public static String getJsonFromList(long recordTotal , List beanList){
TotalJson total = new TotalJson();
total.setResults(recordTotal);
total.setItems(beanList);
JSONObject JsonObject = JSONObject.fromObject(total);
return JsonObject.toString();
}
/**
* 通过List生成JSON数据
* @param beanList 包含bean对象的集合
* @return 生成的JSON数据
*/
@SuppressWarnings("unchecked")
public static String getJsonFromList(List beanList){
return getJsonFromList(beanList.size(),beanList);
}
/**
* 通过bean生成JSON数据
* @param bean bean对象
* @return 生成的JSON数据
*/
public static String getJsonFromBean(Object bean){
JSONObject JsonObject = JSONObject.fromObject(bean);
return JsonObject.toString();
}
}
==================相关的类TotalJson.java==========================
package com.hrm.util;
import java.util.List;
@SuppressWarnings("unchecked")
public class TotalJson {
private long results;
private List items;
public List getItems() {
return items;
}
public void setItems(List items) {
this.items = items;
}
public long getResults() {
return results;
}
public void setResults(long results) {
this.results = results;
}
}
=========================Total.java=======================
package com.hrm.util;
public class Total {
private long results;
public long getResults() {
return results;
}
public void setResults(long results) {
this.results = results;
}
}
======================相关jar==============================
json-lib-2.2.1-jdk15.jar
xstream-1.3.jar
commons的一些相关jar
例如
commons-lang-2.3.jar
commons-logging-1.0.4.jar
commons-beanutils-1.6.jar
commons-collections-3.2.jar
ezmorph-1.0.4.jar
可以使用有相关功能的jar代替
===============调用实例=======================
Struts1.x中Action里调用
public ActionForward getAllDepartment(ActionMapping map, ActionForm form,
HttpServletRequest request, HttpServletResponse response) throws Exception {
BeanFactory bf=new ClassPathXmlApplicationContext("applicationContext-*.xml");
DepartmentDao dptDao=(DepartmentDao)bf.getBean("dptManager");
List<Department> dpts=dptDao.GetAllDpt();
String json=ExtHelper.getJsonFromList(dpts);
response.setContentType("text/json;charset=utf-8");
System.out.println(json);
response.getWriter().write(json);
// String xml=ExtHelper.getXmlFromList(dpts);
// //System.out.println(xml);
// response.setContentType("application/xml;charset=utf-8");
// response.getWriter().write(xml);
return null;
}
注释部分为xml格式的返回
====================Extjs的Store===================
var _store = new Ext.data.JsonStore({
root:'items',
autoLoad : true,
fields:[
{name:'id',mapping:'id'},
{name:'dptNum',mapping:'dptNum'},
{name:'dptName',mapping:'dptName'},
{name:'dptMan',mapping:'dptMan'}
],
proxy : new Ext.data.HttpProxy({
url : 'getAllDepartment.do?method=getAllDepartment'
})
});