我正在使用Struts2 jquery图表插件3.4.0。当我使用json从Action类获取日期值时,我得到空白图表。如果我使用简单的操作,那么相同的代码工作正常。 这是我的jsp代码。
<s:url id="chartDataUrl" action="jsonChartData"/>
<sjc:chart
id="chartDate"
xaxisMode="time"
xaxisTimeformat="%m.%Y"
xaxisMin="%{minTime}"
xaxisMax="%{maxTime}"
xaxisColor="#666"
xaxisTickSize="[3, 'month']"
xaxisTickColor="#aaa"
xaxisPosition="top"
yaxisPosition="right"
yaxisTickSize="10"
cssStyle="width: 600px; height: 400px;"
>
<sjc:chartData
id="chartAjaxData1"
label="Map -Double, Double-"
href="%{chartDataUrl}" // when i remove json call then it works fine
list="dateFromMap"
reloadTopics="reloadMap"
lines="{show : true}"
/>
</sjc:chart>
struts.xml代码
<action name="jsonChartData"
class="com.ebhasin.fitnessbliss.actions.GraphsAction">
<result type="json" name="success"></result>
</action>
动作类代码:
public class GraphsAction extends ActionSupport {
private String currentDate;
private Map<Date, Float> dateFromMap;
HomeService homeService = new HomeService();
SimpleDateFormat formatter = new SimpleDateFormat("dd-MM-yyyy");
@Override
public String execute() {
System.out.println("execute");
float weight;
Date date = new Date();
Map session = ActionContext.getContext().getSession();
Integer loginId = (Integer) session.get("loginId");
if (loginId != null) {
dateFromMap = new TreeMap<Date, Float>();
List list = homeService.getWeightGraphData(loginId);
if (list.size() > 0) {
Iterator itr = list.iterator();
while (itr.hasNext()) {
UserStats userStats = (UserStats) itr.next();
weight = userStats.getWeight();
date = userStats.getCreatedDate();
//currentDate = formatter.format(date);
dateFromMap.put(date, weight);
}
} else {
// dateFromMap.put("my",2F );
}
} else {
}
return SUCCESS;
}
public String getCurrentDate() {
return currentDate;
}
public void setCurrentDate(String currentDate) {
this.currentDate = currentDate;
}
public Map<Date, Float> getDateFromMap() {
return dateFromMap;
}
public void setDateFromMap(Map<Date, Float> dateFromMap) {
this.dateFromMap = dateFromMap;
}
}
答案 0 :(得分:0)
编辑: 请放一个System.out.println(“something”);在每个if / else块上,看看你到底在哪里;
作为注释,the preferred way to get the session object是实现SessionAware,而不是使用ActionContext;
你的JSON Action看起来像这样吗? :
http://struts.jgeppert.com/struts2-jquery-showcase/index.action
(转到右下方标签中的More Widgets -> Charts -> JSON Action
。
@ParentPackage(value = "showcase")
public class JsonChartData extends ActionSupport {
private static final long serialVersionUID = 6659512910595305843L;
private List<ListValue> objList;
private Map<Double, Double> doubleMap;
@Actions( {
@Action(value = "/json-chart-data", results = {
@Result(name = "success", type = "json")
})
})
public String execute()
{
objList = new ArrayList<ListValue>();
doubleMap = new TreeMap<Double, Double>();
Random generator = new Random();
for (int i = 1; i <= 24; i++)
{
doubleMap.put(Double.valueOf("" + i), generator.nextDouble() * 10.0);
}
for (int i = 1; i <= 24; i++)
{
objList.add(new ListValue("" + i, "" + generator.nextInt(30)));
}
return SUCCESS;
}
public String getJSON()
{
return execute();
}
public List<ListValue> getObjList()
{
return objList;
}
public void setObjList(List<ListValue> objList)
{
this.objList = objList;
}
public Map<Double, Double> getDoubleMap()
{
return doubleMap;
}
public void setDoubleMap(Map<Double, Double> doubleMap)
{
this.doubleMap = doubleMap;
}
}