0

我正在使用 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;
    }
}
4

1 回答 1

0

编辑:请放一个 System.out.println("something"); 在每个 if / else 块上查看您经过的确切位置;

注意,获取会话对象的首选方法是实现 SessionAware,而不是使用 ActionContext;


你的 JSON 动作看起来像这样吗?:

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;
  }
}
于 2012-12-04T09:06:45.970 回答