java ztree json 实例_ztree异步加载简单示例(struts2)

package org.masque.ztree.demo.action;

import java.io.IOException;

import java.io.PrintWriter;

import java.util.ArrayList;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONArray;

import net.sf.json.JSONObject;

import org.apache.commons.lang.StringUtils;

import org.apache.struts2.interceptor.ServletRequestAware;

import org.apache.struts2.interceptor.ServletResponseAware;

import org.masque.ztree.demo.vo.ZtreeVo;

import com.opensymphony.xwork2.ActionSupport;

public class JsonTest extends ActionSupport implements ServletRequestAware, ServletResponseAware{

/**

*

*/

private static final long serialVersionUID = 1L;

protected HttpServletRequest request;

protected HttpServletResponse response;

public String getList() {

String id = request.getParameter("id");

String name = request.getParameter("name");

List l = new ArrayList();

if (StringUtils.isEmpty(id)||StringUtils.isEmpty(name)) {

ZtreeVo ztreeVo1 = new ZtreeVo("1", "测试1", true);

ZtreeVo ztreeVo2 = new ZtreeVo("2", "测试2", true);

ZtreeVo ztreeVo3 = new ZtreeVo("3", "测试3", true);

ZtreeVo ztreeVo4 = new ZtreeVo("4", "测试4", true);

l.add(ztreeVo1);

l.add(ztreeVo2);

l.add(ztreeVo3);

l.add(ztreeVo4);

}else{

ZtreeVo ztreeVo1 = new ZtreeVo(id+"_1", "测试"+id+"_1", true);

ZtreeVo ztreeVo2 = new ZtreeVo(id+"_2", "测试"+id+"_2", true);

ZtreeVo ztreeVo3 = new ZtreeVo(id+"_3", "测试"+id+"_3", true);

ZtreeVo ztreeVo4 = new ZtreeVo(id+"_4", "测试"+id+"_4", true);

l.add(ztreeVo1);

l.add(ztreeVo2);

l.add(ztreeVo3);

l.add(ztreeVo4);

}

System.out.println(JSONArray.fromObject(l).toString());

writeJSON(JSONArray.fromObject(l).toString());

return null;

}

public void setServletResponse(HttpServletResponse response) {

this.response = response;

}

public void setServletRequest(HttpServletRequest request) {

this.request = request;

}

protected void writeJSON(String json) {

response.setContentType("application/json");

response.setCharacterEncoding("UTF-8");

PrintWriter writer = null;

try {

writer = response.getWriter();

} catch (IOException e) {

e.printStackTrace();

}

writer.write(json);

writer.flush();

writer.close();

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值