日期:2014-05-16  浏览次数:20344 次

向页面写入json数据
/* 向页面输出json格式的数据
	 public void execute() {
		 Map map = new HashMap(); 
         map.put( "int", new Integer(1) );     
         map.put( "arr", new String[]{"a","b"} );     
         map.put( "func", "function(i){ return this.arr; }" );     
         JSONObject json = JSONObject.fromObject( map );     
		 this.writerJson(json.toString());
	}
	
	private void writerJson(String text) {
		ActionContext context = ActionContext.getContext();
		if (context == null) {
			return;
		}
		HttpServletResponse response = (HttpServletResponse) ActionContext
				.getContext().get(ServletActionContext.HTTP_RESPONSE);
		response.setContentType("application/json;charset=UTF-8");
		response.setCharacterEncoding("UTF-8");
		PrintWriter pw = null;
		try {
			pw = response.getWriter();
		} catch (IOException e) {
			throw new RuntimeException(e);
		}
		pw.write(text);
		pw.flush();
	}