简体   繁体   English

在javascript函数中使用jsp

[英]using jsp in javascript function

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! 这是我的代码: 编辑:该代码在我的.jsp文件中!

    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 competenties',
        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). 由于某种原因,它不会在<%%>之间执行代码(来自jsp)。

This page online: http://project-omega.appspot.com/grafieken.jsp The google app engine logs say the error is on the last line of my page. 在线本页: http : //project-omega.appspot.com/grafieken.jsp谷歌应用引擎日志显示错误位于我页面的最后一行。 It's a nullpointerexception. 这是一个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]
        ]);

These are JSP markups, you cannot use them in JavaScript! 这些是JSP标记,您不能在JavaScript中使用它们!

That's because JSP files are compiled to the .java classes during compilation, and JavaScript is executed on the client side. 这是因为在编译过程中,JSP文件被编译为.java类,而JavaScript在客户端执行。

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. 您可以执行相反的操作-在JSP文件中生成JavaScript代码,这样就可以将所需的一些数据传递给JS变量。

I suppose you haven't set the stellingen request attribute. 我想您尚未设置stellingen请求属性。 You usually set the request attributes in a servlet, before forwarding the request to jsp: 通常,在将请求转发到jsp之前,请在servlet中设置请求属性:

@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: 还要确保在JSP代码中设置了该属性:

<%
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() + "],");       
    }   
}
%> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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