Spring MVC从控制器向JQuery发布数据

时间:2015-09-16 07:07:38

标签: javascript jquery spring spring-mvc

我的春季mvc项目有一张图表。此图表从javascript获取数据。我的数据来自控制器通过modelandview。我可以使用$ {}标签在jsp中读取模型数据。但是这个标签不适用于脚本。所以我不能填写图表。而且我必须使用循环(foreach)来吸引数据。

这是图表的代码。

$('#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;

}

2 个答案:

答案 0 :(得分:0)

在你的页面开始添加变量之类的 将此代码添加到您的jsp页面

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<html>
<head>
<c:set var="x" value="${/*YOUR_MODAL_OBJECT*/}" />
<script type="text/javascript">
    var variable= "<c:out value="${x}" />";
</script>

代码应该在所有java脚本导入之前进行,并且该变量可用于您的java脚本代码。

答案 1 :(得分:0)

不幸的是我们不能在JavaScript中使用EL标签。为了在java脚本中访问模型数据,首先将数据分配给jsp中的某个隐藏字段,并尝试在java脚本函数中访问此隐藏字段数据。

参考:Accessing the expression language in javascript of a jsp page