<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<script language="Javascript" src="http://www.fusioncharts.com/free/demos/Blueprint/FusionCharts/FusionCharts.js"></script>
<link type="text/css" rel="stylesheet" href="js/themes/default/easyui.css">
<link type="text/css" rel="stylesheet" href="js/themes/icon.css">
<script type="text/javascript" src="js/jquery-1.4.min.js"></script>
<script type="text/javascript" src="js/jquery.easyui.min.js"></script>
<script type="text/javascript" src="js/jquery.form.js"></script>
<script type="text/javascript" src="js/easyui-lang-zh_CN.js"></script>
<body>
<div id="chart1div"></div>
<script language="JavaScript">
$(function() {
$.ajax({
type: "POST",
url: 'page!fusionchart.action',
data: {},
success: function(data) {
var areaObject = data;
var strXML = "<graph caption='最烂电影排名' xAxisName='电影名' yAxisName='电影' baseFont='宋体' baseFontSize='12' >";
var length = areaObject.length;
for ( var i = 1; i <= length; i++) {
strXML = strXML + "<set name='"+areaObject[i-1].name+"' value='"+areaObject[i-1].count+"' color='"+areaObject[i-1].color+"'/>";
}
strXML = strXML + "</graph>";
var chart1 = new FusionCharts(
"http://www.fusioncharts.com/free/demos/Blueprint/FusionCharts/FCF_Column3D.swf",
"chart1Id", "900", "400");
chart1.setDataXML(strXML);
chart1.render("chart1div");
}
});
});
/* var areaObject = [ {
name : "三枪",
count : 1100,
color : "008E8E"
}, {
name : "刺陵",
count : 901,
color : "A186BE"
}, {
name : "熊猫大侠",
count : 807,
color : "F6BD0F"
}, {
name : "大兵小将",
count : 41,
color : "AFD8F8"
}, {
name : "我的故事",
count : 51,
color : "B3AA00"
}, {
name : "百分比的爱",
count : 1801,
color : "B3AA00"
}, {
name : "花田囍事2012",
count : 69,
color : "B3AA00"
}, {
name : "全称热恋",
count : 121,
color : "008ED6"
} ];
var strXML = "<graph caption='最烂电影排名' xAxisName='电影名' yAxisName='count99' baseFont='宋体' baseFontSize='12' >";
var length = areaObject.length;
for ( var i = 1; i <= length; i++) {
strXML = strXML + "<set name='"+areaObject[i-1].name+"' value='"+areaObject[i-1].count+"' color='"+areaObject[i-1].color+"'/>";
}
strXML = strXML + "</graph>";
var chart1 = new FusionCharts(
"http://www.fusioncharts.com/free/demos/Blueprint/FusionCharts/FCF_Column3D.swf",
"chart1Id", "900", "400");
chart1.setDataXML(strXML);
chart1.render("chart1div"); */
</script>
</body>
</html>
package com.hys.test;
import java.awt.Color;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionSupport;
public class JsonJqueryAction extends ActionSupport {
private static final long serialVersionUID = 7263568517757245698L;
private JSONObject result;
private JSONArray result2;
/**
* @Title: execute
* @Description: 默认执行方法
* @date 2012-10-24 下午03:01:26
* @return
* @throws Exception
*/
@Override
public String execute() throws Exception {
int rows = Integer.parseInt((String) ServletActionContext.getRequest()
.getParameter("rows"));
int page = Integer.parseInt((String) ServletActionContext.getRequest()
.getParameter("page"));
String itemid = ServletActionContext.getRequest()
.getParameter("itemid");
String productid = ServletActionContext.getRequest().getParameter(
"productid");
List<UserInfo> list = new ArrayList<UserInfo>();
UserInfo user = null;
for (int i = (page - 1) * rows; i < 10; i++) {
user = new UserInfo();
user.setUserId(i);
user.setUserName("userName" + i);
user.setPassword("password" + i);
if (productid != null && !"".equals(productid)) {
if (user.getUserName().indexOf(productid) > -1) {
list.add(user);
}
} else {
list.add(user);
}
}
Map<String, Object> jsonMap = new HashMap<String, Object>();
// ui需要显示数据的总页数
jsonMap.put("total", list.size());
// ui需要实现数据的总记录数
jsonMap.put("rows", list);
// 放统计数据
// Map<String, Object> data = new HashMap<String, Object>();
// data.put("userName", "Total");
// data.put("password", "282.35");
List<Map<String, Object>> optionList = new ArrayList<Map<String, Object>>();
Map<String, Object> oMap = new HashMap<String, Object>();
oMap.put("userName", "Total: 41656.26元");
optionList.add(oMap);
jsonMap.put("footer", optionList);
result = JSONObject.fromObject(jsonMap);
return SUCCESS;
}
/**
* @Title: save
* @Description: 保存信息
* @date 2012-10-24 下午03:01:26
* @return
* @throws Exception
*/
public String save() throws Exception {
Map<String, Object> jsonMap = new HashMap<String, Object>();
jsonMap.put("flag", 1);
jsonMap.put("msg", "保存成功!");
result = JSONObject.fromObject(jsonMap);
return SUCCESS;
}
/**
* @Title: delete
* @Description: 删除信息
* @date 2012-10-24 下午03:01:41
* @return
* @throws Exception
*/
public String delete() throws Exception {
Map<String, Object> jsonMap = new HashMap<String, Object>();
jsonMap.put("flag", 1);
jsonMap.put("msg", "删除成功!");
result = JSONObject.fromObject(jsonMap);
return SUCCESS;
}
/**
* @Title: delete
* @Description: 删除信息
* @date 2012-10-24 下午03:01:41
* @return
* @throws Exception
*/
public String total() throws Exception {
Map<String, Object> jsonMap = new HashMap<String, Object>();
jsonMap.put("flag", 1);
int a = (int) (Math.random() * 1000000);
jsonMap.put("msg", a);
result = JSONObject.fromObject(jsonMap);
return SUCCESS;
}
/**
* @Title: chart
* @Description: 删除信息
* @date 2012-10-24 下午03:01:41
* @return
* @throws Exception
*/
public String chart() throws Exception {
Map<String, Object> jsonMap = new HashMap<String, Object>();
List<String> optionList = new ArrayList<String>();
for (int i = 0; i < 10; i++) {
int a = (int) (Math.random() * 10000);
optionList.add(a + "");
// Map<String, Object> oMap = new HashMap<String, Object>();
// oMap.put("serie"+i, 10);
// optionList.add(oMap);
}
// JSONArray jsonArray = JSONArray.fromObject(optionList);
result2 = JSONArray.fromObject(optionList);
return "arra";
}
/**
* @Title: chart
* @Description: 删除信息
* @date 2012-10-24 下午03:01:41
* @return
* @throws Exception
*/
public String fusionchart() throws Exception {
Color color = new Color(
(new Double(Math.random() * 128)).intValue() + 128,
(new Double(Math.random() * 128)).intValue() + 128,
(new Double(Math.random() * 128)).intValue() + 128);
result2 = new JSONArray();
JSONObject json =null;//声明JSONObject对象
for (int i = 1; i < 13; i++) {
int a = (int) (Math.random() * 1000);
json = new JSONObject();
json.put("name", i+"月");
json.put("count", a+"");
json.put("color", getRandomColorCode());
result2.add(json);
}
return "arra";
}
/**
* 生成随机颜色代码
*
* @return
*/
public static synchronized String getRandomColorCode() {
// 颜色代码位数
int colorLength = 6;
// 颜色代码数组
char[] codeSequence = { 'A', 'B', 'C', 'D', 'E', 'F', '0', '1', '2',
'3', '4', '5', '6', '7', '8', '9' };
// StringBuffer sb = new StringBuffer("#");
StringBuffer sb = new StringBuffer();
Random random = new Random();
for (int i = 0; i < colorLength; i++) {
sb.append(codeSequence[random.nextInt(16)]);
}
return sb.toString();
}
/**
* 获取十六进制的颜色代码.例如 "#6E36B4" , For HTML ,
*
* @return String
*/
public static String getRandColorCode() {
String r, g, b;
Random random = new Random();
r = Integer.toHexString(random.nextInt(256)).toUpperCase();
g = Integer.toHexString(random.nextInt(256)).toUpperCase();
b = Integer.toHexString(random.nextInt(256)).toUpperCase();
r = r.length() == 1 ? "0" + r : r;
g = g.length() == 1 ? "0" + g : g;
b = b.length() == 1 ? "0" + b : b;
return r + g + b;
}
public static void main(String[] args) {
System.out.println(getRandomColorCode());
System.out.println(getRandColorCode());
}
public JSONObject getResult() {
return result;
}
public void setResult(JSONObject result) {
this.result = result;
}
public JSONArray getResult2() {
return result2;
}
public void setResult2(JSONArray result2) {
this.result2 = result2;
}
}
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<package name="struts2" namespace="/" extends="json-default">
<action name="struts2" class="com.hys.test.Struts2Action">
<result type="json"></result>
<result type="json" name="arr"></result>
</action>
</package>
<package name="page" namespace="/" extends="json-default">
<action name="page" class="com.hys.test.JsonJqueryAction">
<result type="json">
<param name="root">result</param>
</result>
<result type="json" name="arra">
<param name="root">result2</param>
</result>
</action>
</package>
</struts>