简体   繁体   中英

Spring MVC posting data to JQuery from controller

I have got a chart for my spring mvc project. This chart get datas from javascript. My datas coming from controller via modelandview. I can read models data in jsp with ${} tags. but this tags doesn't work with script. So i cant fill chart. And also i must use loop(foreach) for attract datas.

Here is chart's code.

$('#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.** 
            }}]

        }]
    });

Here is charts div.

<div id="container" style="min-width: 770px; height: 514px; margin: 0 auto"></div>

And its my 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;

}

In your page starting add the variable like add this code to the your jsp page

<%@ 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>

The code should go before all your java script imports and the variable will be available to your java script code.

Unfortunately we can't use EL tags in JavaScript. In order to access model data in java script, first assign data to some hidden field in jsp and try to access this hidden field data in java script function.

Refer: Accessing the expression language in javascript of a jsp page

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM