json+struts2+ajax+list实现异步传输一个列表

首先引包:很多包,漏了一个就会导致无法运行的,就是停在那个地方,不进行下去了

struts2-json-plugin-2.1.8.1.jar

commons-beanutils-core-1.8.0-BETA.jar

commons-lang-2.4.jar

ezmorph-1.0.6.jar

ok,包引入之后就进行我们的代码了

首先struts2配置文件中

<!-- Add packages here -->
	<package name="ajax" extends="json-default" namespace="/">
			 <action name="json_*" class="jsonAction" method="{1}">
 				  <!-- 将返回类型设置为json -->
     			  <result type="json" ><param name="root">resultTree</param></result>
   				  </action>
	</package>

然后写我们的action

package com.dzy.cms.view.action;

import java.util.List;

import javax.annotation.Resource;

import net.sf.json.JSONArray;
import net.sf.json.JsonConfig;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.dzy.cms.domain.Myclass;
import com.dzy.cms.service.MyclassService;
import com.opensymphony.xwork2.ActionSupport;

@Controller
@Scope("prototype")
public class JsonAction extends ActionSupport{
	
	//这个是用来处理json的后台数据的action
	//既然我们用来接收学院
	@Resource
	protected MyclassService myclassService;
	private String academyid;
	private String resultTree;   //返回给json的东西
	
	public String getResultTree() {
		return resultTree;
	}
	public void setResultTree(String resultTree) {
		this.resultTree = resultTree;
	}
	public String getAcademyid() {
		return academyid;
	}
	public void setAcademyid(String academyid) {
		this.academyid = academyid;
	}
	
	
	public String ajax()
	{
		//ok,接收参数
		//System.out.println(academyid);
		List<Myclass> myclasslist=myclassService.getallclassbyaid(Integer.parseInt(academyid));
		try { 
		//自定义JsonConfig用于过滤Hibernate配置文件所产生的递归数据
		JsonConfig config = new JsonConfig();
		config.setExcludes(new String[]{"classcourses","students","academy"});  //只要设置这个数组,指定过滤哪些字段。   
	    //组成JSON数组
		JSONArray array=JSONArray.fromObject(myclasslist,config);
		this.setResultTree(array.toString());
		//System.out.println(array.toString());
		} catch (Exception e) {
			e.printStackTrace();
		}
		return SUCCESS;
		
	}

}

这里的过滤字段很重要,要不然会死循环什么的报错的


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值