最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

java - using jsp in javascript function - Stack Overflow

programmeradmin1浏览0评论

I've searched for this but can't find anything. Please correct my question if it's incorrect english.

This is my code: EDIT: The code is within my .jsp file!

    function drawChart() {
    var data = google.visualization.arrayToDataTable([
        ['Beoordeling', 'Stage Opleider', 'Student'],

        ['1', '1', '4'],

        <% ArrayList < Stelling > alleStellingenLijst2 = new ArrayList < Stelling > ();
        alleStellingenLijst2 = (ArrayList < Stelling > ) request.getAttribute("stellingen");
        for (Stelling s: alleStellingenLijst2) {
            out.println("['1', '" + s.getDeStelling() + "' , '" + s.getDeWaarde() + "'],");
        } %> ]);
    var options = {
        title: 'Laatste petenties',
        hAxis: {
            title: 'Score',
            titleTextStyle: {
                color: 'green'
            }
        },
        vAxis: {
            title: 'Beoordeling nummer',
            titleTextStyle: {
                color: 'green'
            }
        },
        // Allow multiple simultaneous selections.
        selectionMode: 'multiple',
        colors: ['#BEF781', 'green']
    };
    var chart = new      google.visualization.BarChart(document.getElementById('chart_div'));
    chart.draw(data, options);
}

For some reason, it wont execute the code between the <% %> (from the jsp).

This page online: .jsp The google app engine logs say the error is on the last line of my page. It's a nullpointerexception.

I have no idea what it means and I really hope someone can help me.

Thanks a lot and sorry for my english.

EDIT The rendered output looks as follows

  function drawChart() {
    var data = google.visualization.arrayToDataTable([                                               
      ['Beoordeling', 'Stage Opleider', 'Student'],
      for (Stelling s : alleStellingenLijst2) {
        out.println("['1', '" + s.getDeStelling() + "' , '" + s.getDeWaarde() + "'],");       
    }  
  ]);

NEW CODE:

 function drawChart() {
        var data = google.visualization.arrayToDataTable([
          ['Beoordeling', 'Stage Opleider', 'Student'],

          ['1', 1, 4],

          <%

            ArrayList<Stelling> alleStellingenLijst2 =(ArrayList<Stelling>) getServletContext().getAttribute("stellingen");
            for (Stelling s : alleStellingenLijst2) {
                out.println("['1', " + s.getDeStelling() + " , " + s.getDeWaarde() + "],");       
            }   
        %> 
        ['2', 2, 2]
        ]);

I've searched for this but can't find anything. Please correct my question if it's incorrect english.

This is my code: EDIT: The code is within my .jsp file!

    function drawChart() {
    var data = google.visualization.arrayToDataTable([
        ['Beoordeling', 'Stage Opleider', 'Student'],

        ['1', '1', '4'],

        <% ArrayList < Stelling > alleStellingenLijst2 = new ArrayList < Stelling > ();
        alleStellingenLijst2 = (ArrayList < Stelling > ) request.getAttribute("stellingen");
        for (Stelling s: alleStellingenLijst2) {
            out.println("['1', '" + s.getDeStelling() + "' , '" + s.getDeWaarde() + "'],");
        } %> ]);
    var options = {
        title: 'Laatste petenties',
        hAxis: {
            title: 'Score',
            titleTextStyle: {
                color: 'green'
            }
        },
        vAxis: {
            title: 'Beoordeling nummer',
            titleTextStyle: {
                color: 'green'
            }
        },
        // Allow multiple simultaneous selections.
        selectionMode: 'multiple',
        colors: ['#BEF781', 'green']
    };
    var chart = new      google.visualization.BarChart(document.getElementById('chart_div'));
    chart.draw(data, options);
}

For some reason, it wont execute the code between the <% %> (from the jsp).

This page online: http://project-omega.appspot./grafieken.jsp The google app engine logs say the error is on the last line of my page. It's a nullpointerexception.

I have no idea what it means and I really hope someone can help me.

Thanks a lot and sorry for my english.

EDIT The rendered output looks as follows

  function drawChart() {
    var data = google.visualization.arrayToDataTable([                                               
      ['Beoordeling', 'Stage Opleider', 'Student'],
      for (Stelling s : alleStellingenLijst2) {
        out.println("['1', '" + s.getDeStelling() + "' , '" + s.getDeWaarde() + "'],");       
    }  
  ]);

NEW CODE:

 function drawChart() {
        var data = google.visualization.arrayToDataTable([
          ['Beoordeling', 'Stage Opleider', 'Student'],

          ['1', 1, 4],

          <%

            ArrayList<Stelling> alleStellingenLijst2 =(ArrayList<Stelling>) getServletContext().getAttribute("stellingen");
            for (Stelling s : alleStellingenLijst2) {
                out.println("['1', " + s.getDeStelling() + " , " + s.getDeWaarde() + "],");       
            }   
        %> 
        ['2', 2, 2]
        ]);
Share Improve this question edited Oct 31, 2013 at 14:10 Colinch asked Oct 31, 2013 at 13:27 ColinchColinch 3532 gold badges7 silver badges22 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 4

These are JSP markups, you cannot use them in JavaScript!

That's because JSP files are piled to the .java classes during pilation, and JavaScript is executed on the client side.

You could do the opposite - generate a JavaScript code in the JSP file, that way you could pass some data you want to the JS variables.

I suppose you haven't set the stellingen request attribute. You usually set the request attributes in a servlet, before forwarding the request to jsp:

@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) {
    ArrayList<Stelling> list = ...;
    req.setAttribute("stellingen", list);
    req.getRequestDispatcher("/grafieken.jsp").forward(req, resp);
}

Also make sure the attribute is set in the JSP code:

<%
List<Stelling> stellingen = (List<Stelling>) getServletContext().getAttribute("stellingen");
if(stellingen == null) {
    out.println("stellingen attribute not set!");
}else{
    for (Stelling s : stellingen) {
        out.println("['1', " + s.getDeStelling() + " , " + s.getDeWaarde() + "],");       
    }   
}
%> 
发布评论

评论列表(0)

  1. 暂无评论