怎么从struts把JSON传给前台AJAX啊?

dsbbs7 2009-03-22 03:01:55
把一个List直接转成JSON然后交给前台JS代码
...全文
1377 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
LoginOut 2009-10-23
  • 打赏
  • 举报
回复
List 转换成Json,需要对List进行解析拼装,否则显示的
json是数据库里面的值,而不是键值对格式的

4楼的可以看看
lanhx23 2009-10-21
  • 打赏
  • 举报
回复
顶上去
HNsnopy 2009-03-26
  • 打赏
  • 举报
回复
补充一下,你可以扩展我写的这个类,因为我这个类是前一段时间写的,当时没考虑太多,我现在扩展过了,可以通过set生成JSON,并且能够配置JsonConfig来过滤掉一些容易出现递归异常的属性,懒得打开IDE,我想这个应该够你用的了
HNsnopy 2009-03-26
  • 打赏
  • 举报
回复
首先下载相关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代替
=====================================================
继续写一个类,用于临时使用

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

======================================
写一个通用helper类

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();
}
}

==========================================
调用实例


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格式的返回


===============================
前台代码

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'
})
});
mumu_java 2009-03-23
  • 打赏
  • 举报
回复
可以自己在后台拼好json传,直接

response.setContentType("text/xml;charset=UTF-8");

try {
response.getWriter().write(json.toString());//json是你拼好的json字符串;
} catch (IOException e) {
e.printStackTrace();
}

myairland 2009-03-22
  • 打赏
  • 举报
回复
补充一下1楼,要下一个包jsonplugin.jar,才能使用result为json的返回类型
love_ai87 2009-03-22
  • 打赏
  • 举报
回复
在返回类型中把type定义为json,然后在在属性excludeProperties中说明action里面用到的service,否则会报错

<result name="json" type="json">
<param name="excludeProperties">
usersService,userGroupsService,domainList,houseinfoService,distionaryService
</param>
</result>

52,782

社区成员

发帖
与我相关
我的任务
社区描述
Web 开发 Ajax
社区管理员
  • Ajax
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧