大大们,求助呀!struts2返回json的问题
struts2返回json串在jsp页面的处理:
直接上代码了:
action:中
private String JsonString;
public String getJsonString() {
return JsonString;
}
public void setJsonString(String jsonString) {
JsonString = jsonString;
}
下面是方法中的代码:
JSONArray jsonArray = JSONArray.fromObject(list_dir);
this.JsonString = jsonArray.toString();
<package name="test.action" extends="json-default" namespace="/sm">
<action name="scanPointPath" class="smAction" method="scanPointPath" >
<result name="success" type="json">
<param name="includeProperties">JsonString</param>
</result>
</action>
$.ajax( {
type : "POST",
url : "scanPointPath.action",
dataType:"json",
data : {
"postValue":leavePath
},
aysnc : false,
success : function(data) {
alert("dddd"+data); },
error : function() {
alert("cuowu");
}
});
success : function(data) {
alert("dddd"+data.constructor);//这里可以输出你返回的json的类型
},
var arr = ["xxxxxx","yyyyy"];
for(var i = 0, j = arr.length; i<j; i++){
alert(arr[i]);
}
<action name="scanPointPath" class="smAction" method="scanPointPath" >
<!-- 对于返回json 的action,我这里从来不写东西-->
</action>
public void loginList(){
DistrictBiz db = new DistrictBizImpl();
List<District> listD =db.getAllDistrict();
//使用JSON工具类打印JSON字符串
JsonWriteUtil.writeJson(resp, listD);
}
/**
* 把对象转换成JSON后,再进行打印
* @param obj
*/
public static void writeJson(HttpServletResponse response, Object obj) {
String str = JSON.toJSONString(obj);
try {
response.setContentType("text/html;charset=utf-8");
response.getWriter().write(str);
response.getWriter().flush();
} catch (IOException e) {
e.printStackTrace();
}
}