---
title: "Struts2+JQuery+JSON"
author: "xiajin2080"
date: "2009-12-19T17:45:50Z"
tags: ["Apache", "JSP", "XML", "jQuery", "json"]
categories: ["Web前端"]
slug: "struts2-jquery-json"
draft: false
lastmod: "2009-12-19T17:55:00Z"
id: 552050
url: "https://xiajin2080.iteye.com/blog/552050"
---

1.Action \[code="java"] package com.fayhong.action.basicdata; import java.util.List; import org.apache.struts2.ServletActionContext; import net.sf.json.JSONArray; import com.fayhong.model.Country; import com.fayhong.service.basicdata.BasicDataService; import com.opensymphony.xwork2.Action; import com.opensymphony.xwork2.ActionSupport; public class JsonAction extends ActionSupport{ private BasicDataService basicDataService; private String countryJSON; public String findCountryList() { StringBuffer sb = new StringBuffer(); String number = ServletActionContext.getRequest().getParameter("number"); List countryList = basicDataService.findCountry(null,number); if (countryList.size() &gt; 0) { sb.append("{"); for (int i = 0; i &lt; countryList.size(); i++) { Country country = (Country) countryList.get(i); sb.append(country.getCountryId()); sb.append(":"); sb.append("\\""); sb.append(country.getCountryId()); sb.append("\\""); if (i != (countryList.size() - 1)) sb.append(","); } sb.append("}"); } JSONArray array = new JSONArray().fromObject(countryList); countryJSON=array.toString(); // cityJSON = sb.toString();// 返回的数据 System.out.println(sb.toString()); System.out.println("cityJSON=="+countryJSON); return Action.SUCCESS; } public void setBasicDataService(BasicDataService basicDataService) { this.basicDataService = basicDataService; } public String getCountryJSON() { return countryJSON; } public void setCountryJSON(String countryJSON) { this.countryJSON = countryJSON; } }\[/code] 2.xml \[code="xml"] \[/code] 3.jsp \[code="html"] &lt;%@ page contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%&gt; test jQuery \[/code]