我有一張適合我的spring mvc項目的圖表。該圖表從javascript獲取數據。我的數據通過modelandview來自控制器。我可以使用$ {}標籤閱讀jsp中的模型數據。但這個標籤不適用於腳本。所以我不能填寫圖表。而且我還必須使用循環(foreach)來吸引數據。Spring MVC從控制器向JQuery發佈數據
這是圖表的代碼。
$('#container').highcharts({
chart: {
type: 'column'
},
title: {
text: 'Monthly Average Rainfall'
},
subtitle: {
text: 'Source: WorldClimate.com'
},
xAxis: {
categories: [
'1',
'2',
'3',
'4',
'5',
'6',
'7',
'8',
'9',
'10',
'11',
'12',
'13',
'14',
'15'
]
},
yAxis: {
min: 0,
title: {
text: 'Rainfall (mm)'
}
},
tooltip: {
headerFormat: '<span style="font-size:10px">{point.key}</span><table>',
pointFormat: '<tr><td style="color:{series.color};padding:0">{series.name}: </td>' +
'<td style="padding:0"><b>{point.y:.1f} mm</b></td></tr>',
footerFormat: '</table>',
shared: true,
useHTML: true
},
plotOptions: {
column: {
pointPadding: 0.01,
borderWidth: 0
}
},
series: [{
name: 'Efor Time',
dataVar: [${for (var log in ${logs}) {
**//in here i must add log.worked attributes value to dataVar.**
}}]
}]
});
這是圖表div。
<div id="container" style="min-width: 770px; height: 514px; margin: 0 auto"></div>
而其我的HomeController
@RequestMapping(value = "/ProfilePage", method = RequestMethod.POST)
public ModelAndView profilePage(@RequestParam("username") String username) {
List<WorkLog> list = new ArrayList<WorkLog>();
WorkLog wl = new WorkLog();
wl.setUser_name(username);
list = wl.getMonthlyLogList();
ModelAndView model = new ModelAndView();
model.addObject("logs", list);
model.setViewName("ProfilePage");
return model;
}